Reading a JavaScript error message

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Reading a JavaScript error message

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Your code runs and the console shows: ReferenceError: score is not defined. What does this tell you?
A The error type is ReferenceError and the variable score was used without being declared
B The error type is SyntaxError and the computer cannot read the file
2. The console shows: SyntaxError: Unexpected token ')' at script.js:7. Where should you start looking?
A Line 1 of script.js, because errors always start at the top
B Line 7 of script.js, checking the brackets and punctuation
3. Which line of code would cause this error: TypeError: total is not a function?
A let total = 5; console.log(totl);
B let total = 5; total();
4. Which error type would you see for this code: console.log(playerName); when playerName was never declared?
A ReferenceError
B SyntaxError
5. Which error type would you see for this code: let x = (5 + 2;
A SyntaxError
B ReferenceError

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1ReferenceError: score is not defined Anme is misspelled, so the console shows a ReferenceError: nme is not defined. Change it to name.
2TypeError: player.jump is not a function BThe closing bracket is missing, so the console shows a SyntaxError. Add ) before the semicolon.
3A SyntaxError means the code is written wrongly, such as a missing bracket, so JavaScript cannot run it CThe code tried to call something as a function, but it is not a function (it may be missing or misspelled)
4The line number in the console tells you where to start looking for the problem DThe code uses a variable name that was never declared, so JavaScript cannot find it
5console.log(score; ESyntaxError: missing ) after argument list
6let name = 'Ava'; console.log(nme); Fscript.js:7 Uncaught ReferenceError: total is not defined

Definitions

error message

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
try { console.log(missing); } catch (e) { console.log(e.____); }
2.
try { null.length; } catch (e) { console.log(e._______); }
3.
try { let n = 5; n(); } catch (e) { console.log(e.name === "_________"); }
4.
try { throw new ___________("Bad"); } catch (e) { console.log(e.name); }
5.
try { undefinedFn(); } catch (e) { console.log(e._____); }

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A const score = ;
B console.log(score);
C console.log(score + 5);
Question 2
A let name = 'Mia';
B console.log(nmae);
C console.log('Done');
Question 3
A let count = 3;
B let items = [1, 2];
C items.push(count);
D count(items);
Question 4
A const pet = 'dog';
B console.log(pet);
C let big = pet.toUppercase();
Question 5
A let age = 15;
B if (age >= 14 {
C console.log('Teen');
D }

Answer Key

Reading a JavaScript error message
Section A: Quick Questions
Q1
A — The error type is ReferenceError and the variable score was used without being declared
Q2
B — Line 7 of script.js, checking the brackets and punctuation
Q3
B — let total = 5; total();
Q4
A — ReferenceError
Q5
A — SyntaxError
Section B: Match the Pairs
Q1
ReferenceError: score is not defined → The code uses a variable name that was never declared, so JavaScript cannot find it
Q2
TypeError: player.jump is not a function → The code tried to call something as a function, but it is not a function (it may be missing or misspelled)
Q3
A SyntaxError means the code is written wrongly, such as a missing bracket, so JavaScript cannot run it → SyntaxError: missing ) after argument list
Q4
The line number in the console tells you where to start looking for the problem → script.js:7 Uncaught ReferenceError: total is not defined
Q5
console.log(score; → The closing bracket is missing, so the console shows a SyntaxError. Add ) before the semicolon.
Q6
let name = 'Ava'; console.log(nme); → nme is misspelled, so the console shows a ReferenceError: nme is not defined. Change it to name.
Section C: Definitions
Q1
error message — The console text that names the error type, such as SyntaxError or TypeError, and the line number where it happened, so you know what went wrong and where to look.
Section D: Fill in the Blanks
Q1
try { console.log(missing); } catch (e) { console.log(e.name); }
Q2
try { null.length; } catch (e) { console.log(e.message); }
Q3
try { let n = 5; n(); } catch (e) { console.log(e.name === "TypeError"); }
Q4
try { throw new SyntaxError("Bad"); } catch (e) { console.log(e.name); }
Q5
try { undefinedFn(); } catch (e) { console.log(e.stack); }
Section E: Spot the Error
Q1
A — The console would report a SyntaxError (Unexpected token ';') on line 1. The variable has no value after the = sign. It should be const score = 10;
Q2
B — The console would report ReferenceError: nmae is not defined, pointing at line 2. The variable name is misspelled. It should be console.log(name);
Q3
D — The console would report TypeError: count is not a function on line 4. count holds a number, and the round brackets try to call it. It should be console.log(items);
Q4
C — The console would report TypeError: pet.toUppercase is not a function on line 3. JavaScript is case sensitive, so the method name must be toUpperCase.
Q5
B — The console would report a SyntaxError on line 2 because the closing bracket is missing. It should be if (age >= 14) {