Reading an error message and its line number

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

This Reading an error message and its line number worksheet is part of the ACARA Digital Technologies Years 7–8 — Programming with Control Structures and Functions (JavaScript) journey. Print it as a booklet?

Reading an error message and its line number

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. An error message says 'script.js:5'. What does the 5 tell you?
A The program has exactly 5 errors in it
B JavaScript noticed the error on line 5, so check there first
2. Which part of an error message names the kind of problem, such as SyntaxError or ReferenceError?
A The line number, at the end of the file name
B The error name, at the start of the message
3. The console shows 'script.js:4 Uncaught SyntaxError: Unexpected token'. Where should you start looking?
A Line 4, and then the line above it
B Line 1, because errors always start there
4. Line 2 is: let name = "Sam and line 3 is: console.log(name);. The error is reported on line 3. Which is true?
A The real mistake may be on line 2, the missing closing quote
B The real mistake must be on line 3, because that is where the error was reported
5. An error message says 'ReferenceError: score is not defined'. What does the description tell you?
A JavaScript could not find a variable called score
B The variable score has the wrong data type

Match the Pairs

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

Left columnRight column
1script.js:3 AThe closing } is missing. JavaScript reports the error at the end of the file (Unexpected end of input), not on line 1 where the function started.
2ReferenceError: score is not defined BThe file name and line number where JavaScript noticed the error. Check line 3 first, but the real mistake may be a line earlier.
3An error message names the type of error and describes what went wrong CTypeError: Assignment to constant variable.
4The real mistake may be a line earlier than the line number in the error message DThe message says ReferenceError: agee is not defined at line 2. The variable name is misspelled and should be age.
5let age = 12; console.log(agee); Elet nums = [1, 2 console.log(nums);
6function hi() { console.log("Hi"); hi(); FThe error name and description: JavaScript cannot find anything called score, so check for a typo or a missing let.

Definitions

script.js:3

Fill in the Blanks

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

1.
console.log(scoree); // ______________: scoree is not defined
2.
let age = 12; age = ; // Error is reported at script.js:_
3.
const max = 5; max = 6; // _________ : Assignment to constant variable.
4.
// Error at script.js:5 // Check line 5 first, then line _
5.
console.log(x); // _________:1 ReferenceError

Spot the Error

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

Question 1
A let score = 10;
B let bonus = 5;
C console.log(score + bonus;
Question 2
A const name = "Mia";
B console.log("Hi + name);
C console.log(name.length);
Question 3
A let total = 0;
B total = total + 5;
C console.log(totl);
Question 4
A const lives = 3;
B let score = 0;
C score += 10;
D lives = lives - 1;
Question 5
A let items = ["pen", "ruler";
B console.log(items[0]);
C console.log(items.length);

Answer Key

Reading an error message and its line number
Section A: Quick Questions
Q1
B — JavaScript noticed the error on line 5, so check there first
Q2
B — The error name, at the start of the message
Q3
A — Line 4, and then the line above it
Q4
A — The real mistake may be on line 2, the missing closing quote
Q5
A — JavaScript could not find a variable called score
Section B: Match the Pairs
Q1
script.js:3 → The file name and line number where JavaScript noticed the error. Check line 3 first, but the real mistake may be a line earlier.
Q2
ReferenceError: score is not defined → The error name and description: JavaScript cannot find anything called score, so check for a typo or a missing let.
Q3
An error message names the type of error and describes what went wrong → TypeError: Assignment to constant variable.
Q4
The real mistake may be a line earlier than the line number in the error message → let nums = [1, 2 console.log(nums);
Q5
let age = 12; console.log(agee); → The message says ReferenceError: agee is not defined at line 2. The variable name is misspelled and should be age.
Q6
function hi() { console.log("Hi"); hi(); → The closing } is missing. JavaScript reports the error at the end of the file (Unexpected end of input), not on line 1 where the function started.
Section C: Definitions
Q1
script.js:3 — Part of an error message: the file and line number where JavaScript noticed the error. Check that line first, but the real mistake may be a line earlier.
Section D: Fill in the Blanks
Q1
console.log(scoree); // ReferenceError: scoree is not defined
Q2
let age = 12; age = ; // Error is reported at script.js:2
Q3
const max = 5; max = 6; // TypeError : Assignment to constant variable.
Q4
// Error at script.js:5 // Check line 5 first, then line 4
Q5
console.log(x); // script.js:1 ReferenceError
Section E: Spot the Error
Q1
C — The closing bracket is missing, so JavaScript reports a SyntaxError and gives line 3 as the place to look. It should be console.log(score + bonus);
Q2
B — The closing quote after Hi is missing, so the string never ends. The SyntaxError (Invalid or unexpected token) points to line 2. It should be console.log("Hi " + name);
Q3
C — The variable name is misspelt, so JavaScript throws a ReferenceError: totl is not defined, and names line 3. It should be console.log(total);
Q4
D — lives is a const, so it cannot be reassigned. JavaScript shows TypeError: Assignment to constant variable on line 4. It should create a new variable: let left = lives - 1;
Q5
A — The closing square bracket is missing on line 1. The error message may point at the end of this line or even a line later, so check the line before it too. It should be let items = ["pen", "ruler"];