ReferenceError: a name that is not defined

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

ReferenceError: a name that is not defined

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. let score = 10; console.log(Score); What happens?
A It prints 10
B ReferenceError: Score is not defined
2. Which line causes a ReferenceError?
A let lives = 3; console.log(live);
B let lives = 3; console.log(lives);
3. What is the cause of this error? console.log(total); let total = 5;
A The number 5 is the wrong data type
B The variable is used before it is declared
4. if (true) { let bonus = 5; } console.log(bonus); What happens?
A ReferenceError: bonus is not defined
B It prints 5
5. Which message tells you a name has not been declared?
A ReferenceError: playerName is not defined
B SyntaxError: Unexpected token '}'

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 AThe name totl is misspelt, so JavaScript cannot find a variable with that name
2let score = 5; console.log(Score); Bconsole.log(lives); let lives = 3;
3Using a variable outside the block where it was declared with let causes a ReferenceError Cif (true) { let bonus = 10; } console.log(bonus);
4Using a variable before declaring it can cause a ReferenceError DThe variable is declared as playerName but used as playername, so the capital N is missing and the name is not defined
5let playerName = 'Mia'; console.log(playername); ECauses a ReferenceError because Score with a capital S is a different name from score
6let total = 4; console.log(totl); FThe program used the name score, but no variable or function called score has been declared

Definitions

ReferenceError

Fill in the Blanks

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

1.
let coins = 10; console.log(_____);
2.
___ speed = 9; console.log(speed);
3.
___ msg = ""; if (true) { msg = "Hi"; } console.log(msg);
4.
function sayHi() { console.log("Hi"); } _____();
5.
let score = 0; score = score + 1; console.log(_____);

Spot the Error

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

Question 1
A let score = 10;
B score += 5;
C console.log(scor);
Question 2
A let score = 20;
B let bonus = 5;
C console.log(Score + bonus);
Question 3
A let Age = 12;
B age++;
C console.log(age);
Question 4
A let width = 4;
B let hieght = 3;
C let area = width * height;
D console.log(area);
Question 5
A let total = 0;
B for (let i = 1; i <= 3; i++) {
C total = total + j;
D }
E console.log(total);

Answer Key

ReferenceError: a name that is not defined
Section A: Quick Questions
Q1
B — ReferenceError: Score is not defined
Q2
A — let lives = 3; console.log(live);
Q3
B — The variable is used before it is declared
Q4
A — ReferenceError: bonus is not defined
Q5
A — ReferenceError: playerName is not defined
Section B: Match the Pairs
Q1
ReferenceError: score is not defined → The program used the name score, but no variable or function called score has been declared
Q2
let score = 5; console.log(Score); → Causes a ReferenceError because Score with a capital S is a different name from score
Q3
Using a variable outside the block where it was declared with let causes a ReferenceError → if (true) { let bonus = 10; } console.log(bonus);
Q4
Using a variable before declaring it can cause a ReferenceError → console.log(lives); let lives = 3;
Q5
let playerName = 'Mia'; console.log(playername); → The variable is declared as playerName but used as playername, so the capital N is missing and the name is not defined
Q6
let total = 4; console.log(totl); → The name totl is misspelt, so JavaScript cannot find a variable with that name
Section C: Definitions
Q1
ReferenceError — An error shown when code uses a variable or function name that has not been declared, often from a typo, wrong capital letter, wrong scope or using it before declaring it.
Section D: Fill in the Blanks
Q1
let coins = 10; console.log(coins);
Q2
let speed = 9; console.log(speed);
Q3
let msg = ""; if (true) { msg = "Hi"; } console.log(msg);
Q4
function sayHi() { console.log("Hi"); } sayHi();
Q5
let score = 0; score = score + 1; console.log(score);
Section E: Spot the Error
Q1
C — scor is a misspelt name. The variable was declared as score, so JavaScript throws ReferenceError: scor is not defined. It should be console.log(score);
Q2
C — JavaScript is case sensitive, so Score and score are different names. Only score was declared, so Score causes a ReferenceError. Use score.
Q3
A — This declares Age with a capital A, but the next lines use age. The name age is never declared, so line 2 throws a ReferenceError. It should be let age = 12;
Q4
B — The variable is declared as hieght, but line 3 uses height. Because height was never declared, JavaScript throws ReferenceError: height is not defined. It should be let height = 3;
Q5
C — The loop counter is called i. The name j was never declared, so it causes a ReferenceError. It should be total = total + i;