Skip to content
FORGENO ADS GAMESUnlock all $8

JavaScript · Unit 3: Working with data · Lesson 5 of 6

Reading errors and debugging

An error message is a clue, not a failure.

Opens this lesson in Code Academy. Lessons open one after another, and each unit opens when you pass the checkpoint quiz before it (you can take a checkpoint early to skip ahead). If this lesson is not open yet, the button shows you its unit on your map.

The idea

An error message is a clue, not a failure. It says what went wrong and on which line. Read the first one first.

Common ones: ReferenceError means a name does not exist, often because of a typo. TypeError means a value was used the wrong way, like calling something that is not a function. SyntaxError means JavaScript could not read the code at all: a missing bracket or quote.

When the result is wrong but there is no error, print values along the way with console.log to find where they stop matching what you expect.

Worked example

JavaScript

const total = 5;
console.log(totl);

It prints

ReferenceError: totl is not defined (line 2)

Run it. The message names the problem and the line; the fix is the spelling.

Key terms

Error messages
ReferenceError: unknown name. TypeError: wrong kind of value. SyntaxError: code JavaScript cannot read.
Debugging
Finding why code misbehaves: read errors, then print values with console.log to see where they go wrong.

Practise it in Code Academy

It has 4 exercises (multiple choice, predict the output and fix the broken code) and takes about 7 minutes. Every answer is checked as you go, and feedback names the exact mistake. Hints and the solution are there if you get stuck.

Every lesson is free: no ads, no lives and no paywall.