Chapter 1 · Foundations
Reading errors
Step 1 of 22 · Concept
Everyone gets errors
By the end of this lesson you can read what Node says when a program is wrong, and fix it.
Every programmer makes mistakes. Node's messages look strange at first, but they say what is wrong and where. You will:
- read Node's messages, and fix one error at a time,
- read the stack when a program stops in the middle,
- find the mistakes that print no error at all.
Step 2 of 22 · Concept
Two kinds of error
| kind | when it stops | what you see |
|---|---|---|
SyntaxError | before any line runs | no output, only the error |
| uncaught error (nothing catches it, lesson 18) | while the program runs | the output so far, then the error |
Under the output, Run adds [exit 1 · ...]: the exit code. It is 1 after either kind of error, 0 when the program
ended well.
Step 3 of 22 · Quiz
Did it run?
You press Run on this program. What does it print before the error?
console.log("Start");
console.log("Middle");
console.log("End";
Step 4 of 22 · Demo
A missing bracket
Run it: nothing is printed. Find the line of the message that starts with SyntaxError:
SyntaxError: missing ) after argument list.
Step 5 of 22 · Concept
Read the message
Read the SyntaxError line first: the kind of error, then what is wrong. Then go to the top:
- the file and the line:
file:///home/student/05-errors/paren/main.js:3, - the line itself, as you wrote it,
- carets,
^, under the place where Node noticed.
Node's own lines come last. This course writes the address short: main.js:3.
Step 6 of 22 · Concept
Three messages you will meet
| message | what is usually wrong |
|---|---|
missing ) after argument list | a ) is missing |
Invalid or unexpected token | a text has no closing " |
Unexpected end of input | a } is missing |
The carets mark where Node noticed, which is not always where the mistake is.
A missing } is noticed only at the end of the file. So Node names the file's end, with no carets, not where the }
belongs.
Step 7 of 22 · Quiz
What does Node mean?
Node stops with this message. What is wrong?
main.js:1
console.log("Hi);
^^^^^
SyntaxError: Invalid or unexpected token
Step 8 of 22 · Concept
One error at a time
console.log("one";
console.log("two");
console.log("three);
Two lines are wrong, but Node stops with one message:
main.js:1
console.log("one";
^^^^^
SyntaxError: missing ) after argument list
- Node reports one syntax error per run: the first in the file.
- Fix it and run again. Then Node reports the next one.
Step 9 of 22 · Exercise
Your turn: two mistakes
This program has two mistakes. Fix the one Node names, run again, and fix the next. Then add a fourth line, so that it prints:
Ready
Set
Go!
Done
Node names line 1 first: Invalid or unexpected token. The mistake on line 3 waits for the next run.
Line 1 needs a " before its ). Run again: now Node says missing ) after argument list and names line 3.
console.log("Go!");
Step 10 of 22 · Concept
Some errors stop the program in the middle
These errors come while the program runs, and nothing catches them, so the program stops:
| error | a typical cause |
|---|---|
ReferenceError: x is not defined | a name Node does not know: a typo |
TypeError: x is not a function | brackets after a value that is no function: name() |
TypeError: Cannot read properties of undefined (reading 'length') | a . after a value that was never set |
Step 11 of 22 · Demo
A crash in the middle
This program has a function, share, as lesson 2 had greet. share(12, 4) runs it with total 12 and people 4;
return gives back its answer (lesson 7). toFixed(2) gives the text "12.00", and / turns it into a number.
Run it. Sharing... and 3 print, then the program stops. Find the line that starts with TypeError.
Step 12 of 22 · Concept
Read the stack
When it stopped, share's frame was on top of the module's frame. After the error, Node lists the frames, the
innermost first:
at share (main.js:2:16): the frame that failed, at line 2, column 16,at main.js:6:13: the module's frame, at the call on line 6,- then Node's own
node:internallines, andNode.js v24.18.1.
This list is a stack trace. Read the error first, then the first at line that names your file.
Step 13 of 22 · Quiz
Who passed the undefined?
The program stopped with this message. share failed because total was undefined. Which line gave it that
undefined?
main.js:2
return total.toFixed(2) / people;
^
TypeError: Cannot read properties of undefined (reading 'toFixed')
at share (main.js:2:16)
at main.js:6:13
function share(total, people) {
return total.toFixed(2) / people;
}
console.log("Sharing...");
console.log(share(12, 4));
console.log(share(undefined, 4));
console.log("never");Step 14 of 22 · Try it
Try it: blame the caller
The second bill is 20, for 5 people. Run it: it crashes. share itself is right.
The at line under at share names the line of the call. Give that call its total, 20.
Sharing...
3
4
at main.js:6:13 points at line 6. Put the total, 20, in place of undefined.
Step 15 of 22 · Concept
A misspelt name
const total = 5;
console.log("Start");
console.log(totl);
Start
main.js:3
console.log(totl);
^
ReferenceError: totl is not defined
The ^ sits under the name Node does not know. Node gives no "did you mean": compare the name with the one you made,
letter by letter. Capitals count too: Console is not console (lesson 1).
Step 16 of 22 · Quiz
Which fix?
This program stops with a ReferenceError. Which fix makes it print 5?
const total = 5;
console.log(totl);
ReferenceError: totl is not defined
const total = 5;
console.log(totl);ReferenceError: totl is not definedStep 17 of 22 · Concept
Mistakes that print no error
Some mistakes stop nothing. The program runs on and prints a wrong answer:
| you see | it came from |
|---|---|
53 | "5" + 3: text joined, not added |
NaN | "abc" * 2: text that is not a number |
undefined | a let never given a value |
With no error, there is no line number. Log the values one by one to find where it went wrong.
Step 18 of 22 · Demo
A total that is wrong
Run it: Total: 53, and no error. The next two lines log each value.
console.log prints text without its quotes. In Run only the colour differs: the number 3 has the number colour,
the text 5 does not. typeof says it in words.
Step 19 of 22 · Exercise
Your turn: find it
This program prints Price: NaN. Find the mistake and fix it:
- Log
price,countandfeeon one line, to see which isNaN. - Fix the text that
Numbercannot read. - Remove your logging line. The program must print:
Price: 9
No error means no line number to go to. One NaN in a sum makes the whole total NaN: find the value it started from.
Put console.log(price, count, fee); after line 3. It prints 2.5 3 NaN: which text became the NaN? JavaScript writes decimals with a point, never a comma.
const fee = Number("1.50");
Step 20 of 22 · Check yourself
Check yourself: pizza for everyone
This program has three mistakes. Node finds one before any line runs and one while it runs; the third prints no error at all. Fix them one run at a time, so that it prints:
Pizzas: 3
Slices: 26
Each: 6.5
Each run shows the next error. When none is left, compare the output with the expected lines: one value is text where a number belongs.
Step 21 of 22 · Challenge · optional
Challenge: your own message
Optional.
share checks people first: the block after if (people === 0) runs only when people is 0 (lesson 6 teaches
if). Make the program stop there with your own message, so that Node prints:
Error: people must be more than 0
Run it: the message prints, then share goes on and divides by 0, so it logs Each: Infinity. It must stop at the message instead.
throw stops the program with an error you make: throw new Error("...") makes an Error with your text (lesson 18 explains it). It takes the place of the console.log.
throw new Error("...");
Step 22 of 22 · Recap
Recap
- A
SyntaxErrorstops Node before any line runs, an uncaught error in the middle. Both exit with 1. - Read the error line first, then the file and the line:
main.js:3. - One syntax error per run: fix it, then run again.
- A stack trace lists the frames, the innermost first. The fix is often in the caller.
53,NaNandundefinedcan come with no error: log the values to find where.
Next: lesson 6 makes programs decide and repeat.
Review
4 questions from this lesson, in a new order each time. Answer from memory: 3 of 4 right on the first try passes. On a day after you finish the lesson, a pass earns 15 XP.
The review needs JavaScript.
console.log("Start");
console.log("Middle");
console.log("End";
console.log("Ready);
console.log("Set");
console.log("Go!";
function share(total, people) {
return total.toFixed(2) / people;
}
console.log("Sharing...");
console.log(share(12, 4));
console.log(share(undefined, 4));
console.log("never");
function share(total, people) {
return total.toFixed(2) / people;
}
console.log("Sharing...");
console.log(share(12, 4));
console.log(share(undefined, 5));
const apples = "5";
const pears = 3;
console.log("Total:", apples + pears);
console.log(apples, pears);
console.log(typeof apples, typeof pears);
const price = Number("2.50");
const count = Number("3");
const fee = Number("1,50");
const total = price * count + fee;
console.log("Price:", total);
const pizzas = 3;
const guests = 4;
const extra = "2"; // 2 extra slices, given as text
const slices = pizzas * 8 + extra;
console.log("Pizzas:", pizzas;
console.log("Slices:", slices);
console.log("Each:", slices / gusts);
function share(total, people) {
if (people === 0) {
console.log("people must be more than 0");
}
return total / people;
}
console.log("Each:", share(12, 4));
console.log("Each:", share(12, 0));