Warm-up · Activity 1 of 7
// B1.3 · ~30 min · Beginner
Reading error messages
After this lesson you can read an error report, tell from its name what to look for, go to the exact line and column, and fix a program one error at a time.
You will be able to
- Tell SyntaxError, ReferenceError and TypeError apart, and know which stops a file before it runs
- Read a report: the ^ under the code, the Name: message and main.js:line:column
- Fix a program one error at a time, and find a logic error that gives no message
Predict · Activity 2 of 7
Predict before you read on: running this program stops with ReferenceError: totl is not defined. The at line below it ends in main.js, a line and a column. Which ones?
let price = 4; let total = price * 2; console.log(totl);Practice · Activity 3 of 7
This line gives SyntaxError: Invalid or unexpected token. Fill in the one character that fixes it, so it prints Total: 12.
console.log("Total:____, 12);console.log("Total:, 12);Practice · Activity 4 of 7
Which kind of error does this program stop with?
let word; console.log(word.length);Practice · Activity 5 of 7
Match each message to the mistake it usually points to.
Brain teaser · Activity 6 of 7
Brain teaser. Both lines have a mistake. Which error does running the program report?
console.log(totl); console.log("done";Apply · Activity 7 of 7
Mini-task: break it on purpose. Write a working program of four lines with a const, a let that is reassigned, and a console.log. Then make three mistakes, one at a time: remove a closing quote, misspell a variable, reassign the const. Before each run, predict the error name; after it, note the message and the line and column. Undo each mistake before the next.
Check your work against this list
Build it yourself
Read the worked example, then write the exercises. Your code runs in your browser or on your computer and is never uploaded.
Worked example
Hunting a logic error with console.log
This program should print the average of three test scores, 80, but its first line prints 180, with no error message at all. Instead of guessing, it prints the values in between: the sum and the third score divided by 3. Those show that only the third score was divided, so the fixed line puts the sum in brackets.
main.js
// Average of three test scores. It should be 80.
const first = 60;
const second = 90;
const third = 90;
const wrongAverage = first + second + third / 3;
console.log("wrong average:", wrongAverage);
console.log("sum:", first + second + third);
console.log("third / 3:", third / 3);
const average = (first + second + third) / 3;
console.log("average:", average);
Run it with
node main.jsOutput
wrong average: 180
sum: 240
third / 3: 30
average: 80- A logic error runs to the end: the only sign is a result that differs from what you worked out by hand.
- 60 + 90 + 30 is 180, so the values in between explain the wrong result: division happened before addition.
- Label each value you print, such as "sum:", so you can tell the lines apart.
- Once the result is right, delete the extra console.log lines.
Change it and run it
Tab indents and Shift+Tab outdents. To leave the editor with the keyboard, press Esc, then Tab.
The first run downloads the JavaScript runner (up to 0.1 MB) and keeps it cached. Your code runs in your browser’s own engine and stays on your device.
Exercises
Exercise 1 of 2
Three errors, one run at a time
This receipt program has three mistakes, each giving a different kind of error. Run it, fix the line the report points to, and run it again until it works. Do not change what it prints: Item: Notebook, Count: 3, Total: 13.5, then Item: Pen and Total: 4.5.
Tab indents and Shift+Tab outdents. To leave the editor with the keyboard, press Esc, then Tab.
The first run downloads the JavaScript runner (up to 0.1 MB) and keeps it cached. Your code runs in your browser’s own engine and stays on your device.
Hints
Hint 1
The first run reports a SyntaxError: missing ) after argument list, on line 4.
Hint 2
The second run reports a ReferenceError. Compare the name in the message with the name on line 1–3, letter by letter.
Hint 3
The third run reports a TypeError on line 7: a variable that is reassigned needs let.
Show a solution
One way to solve it. Yours can look different and still pass the checks.
let item = "Notebook";
let price = 4.5;
let count = 3;
console.log("Item:", item);
console.log("Count:", count);
console.log("Total:", price * count);
item = "Pen";
price = 1.5;
console.log("Item:", item);
console.log("Total:", price * count);
Run it on your computer
Install ECMAScript 2026 or newer. Save these files in one folder, open a terminal in that folder, and run the commands below.
main.js
const item = "Notebook";
let price = 4.5;
let count = 3;
console.log("Item:", item;
console.log("Count:", Count);
console.log("Total:", price * count);
item = "Pen";
price = 1.5;
console.log("Item:", item);
console.log("Total:", price * count);
main.test.js
import {test} from 'node:test';
import assert from 'node:assert/strict';
import {runMain} from './learnrun.js';
test('runs without an error', async () => {
await runMain();
});
test('prints the whole receipt', async () => {
const got = (await runMain()).trimEnd().split('\n');
assert.deepEqual(got, ['Item: Notebook', 'Count: 3', 'Total: 13.5', 'Item: Pen', 'Total: 4.5'], `the program printed ${JSON.stringify(got)}`);
});
package.json
{
"type": "module"
}
package.json tells Node.js that the .js files are modules; keep it in the folder.
Run the program:
node main.jsRun the checks (needs learnrun.js in the same folder):
node --testDownload learnrun.jsExercise 2 of 2
No message, wrong result
This program converts Celsius to Fahrenheit, but it prints 25 C is 82.6 F and 100 C is 180 F, with no error. The right results are 77 and 212: multiply by 9, divide by 5, then add 32. Print values in between if that helps, find the two wrong lines and fix them.
Tab indents and Shift+Tab outdents. To leave the editor with the keyboard, press Esc, then Tab.
The first run downloads the JavaScript runner (up to 0.1 MB) and keeps it cached. Your code runs in your browser’s own engine and stays on your device.
Hints
Hint 1
Work out 25 * 9 / 5 + 32 by hand first: 45 + 32 = 77.
Hint 2
On line 2, 32 * 9 / 5 is worked out before the +: multiplication and division come before addition.
Hint 3
Line 5 forgets one step of the formula altogether.
Show a solution
One way to solve it. Yours can look different and still pass the checks.
let celsius = 25;
let fahrenheit = celsius * 9 / 5 + 32;
console.log(celsius, "C is", fahrenheit, "F");
celsius = 100;
fahrenheit = celsius * 9 / 5 + 32;
console.log(celsius, "C is", fahrenheit, "F");
Run it on your computer
Install ECMAScript 2026 or newer. Save these files in one folder, open a terminal in that folder, and run the commands below.
main.js
let celsius = 25;
let fahrenheit = celsius + 32 * 9 / 5;
console.log(celsius, "C is", fahrenheit, "F");
celsius = 100;
fahrenheit = celsius * 9 / 5;
console.log(celsius, "C is", fahrenheit, "F");
main.test.js
import {test} from 'node:test';
import assert from 'node:assert/strict';
import {runMain} from './learnrun.js';
const lines = async () => (await runMain()).trimEnd().split('\n');
test('converts 25 degrees', async () => {
const got = await lines();
assert.equal(got[0], '25 C is 77 F', `the first line is ${JSON.stringify(got[0])}, expected "25 C is 77 F"`);
});
test('converts 100 degrees', async () => {
const got = await lines();
assert.equal(got[1], '100 C is 212 F', `the second line is ${JSON.stringify(got[1])}, expected "100 C is 212 F"`);
});
package.json
{
"type": "module"
}
package.json tells Node.js that the .js files are modules; keep it in the folder.
Run the program:
node main.jsRun the checks (needs learnrun.js in the same folder):
node --testDownload learnrun.jsCommon mistakes
Looking only at the reported line
const fruits = ["apple", "pear",
console.log(fruits);
What Node.js prints
SyntaxError: Unexpected token ';'Why, and the fix
The report points to line 2, but line 2 is fine. The list on line 1 is never closed, so JavaScript reads line 2 as part of the list and gives up at the ; it did not expect there. When the reported line looks right, read the lines above it for an unclosed ], ) or quote. Here: const fruits = ["apple", "pear"];
Using a variable that has no value yet
let word;
console.log(word.length);
What Node.js prints
TypeError: Cannot read properties of undefined (reading 'length')Why, and the fix
The message names both parts: the value, undefined, and what was asked of it, length. word was declared without a value, so it holds undefined, which has no properties. Give the variable a value before you use it, such as let word = "hello";.
A string with no closing quote
console.log("Hello);
What Node.js prints
SyntaxError: Invalid or unexpected tokenWhy, and the fix
The message does not mention quotes, which is why it is worth knowing: V8 says Invalid or unexpected token when a string runs to the end of the line without its closing quote. Look for a string that starts with " or ' and check it ends with the same character: console.log("Hello");
JavaScript in the browser: your browser’s own engine, in a sandboxed worker. Syntax errors are located with acorn 8.18.0, MIT. Licence and source
Exit ticket
5 questions, no hints. Score 80% or more to complete the lesson.
Finish every activity above to unlock the exit ticket.