Reassigning a let variable

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

Reassigning a let variable

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A game starts with let score = 0; Which line gives score a new value of 10?
A let score = 10;
B score = 10;
2. What happens when you run this code? let lives = 3; let lives = 2;
A Error: lives has already been declared
B lives is now 2
3. After this code runs, what is score? let score = 5; score = 8;
A 8
B 13
4. Which line adds 1 to score by using its old value?
A score = score + 1;
B let score = score + 1;
5. let score = 4; score = score + 1; What is score now?
A 1
B 5

Match the Pairs

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

Left columnRight column
1score = 10; Ascore + 1 works out an answer but never stores it, so score is still 4. There is no = to reassign it.
2score = score + 1; Blet lives = 3; lives = 2;
3To change a let variable that already exists, use its name and = only, with no let in front. CWorks out the old score plus 1 first, then stores that new value back in score.
4A new value can be built from the old value, because the right side is worked out before it is stored. Dlet score = 5; score = score + 2;
5let score = 0; let score = 10; EReplaces the old value of the existing let variable score with 10, without writing let again.
6let score = 4; score + 1; Flet is written twice for the same name, which causes a SyntaxError because score is already declared.

Definitions

score = score + 1;

Fill in the Blanks

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

1.
let score = 10; _____ = 20; console.log(score);
2.
let score = 10; score = score _ 1; console.log(score);
3.
___ lives = 3; lives = 2; console.log(lives);
4.
let coins = 5; coins = _____ * 2; console.log(coins);
5.
let points = 4; points _ points + 3; console.log(points);

Spot the Error

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

Question 1
A let score 0;
B score = 10;
C score = score + 1;
D console.log(score);
Question 2
A let lives = 3;
B let lives = lives - 1;
C console.log(lives);
Question 3
A let coins = 20;
B coins = coins + 5;
C console.log(coins);
D let coins = coins * 2;
E console.log(coins);
Question 4
A let score = 0;
B score = score + 1;
C score = score + 1;
D score + 1;
E console.log(score);
Question 5
A let points = 10;
B points + 5 = points;
C console.log(points);

Answer Key

Reassigning a let variable
Section A: Quick Questions
Q1
B — score = 10;
Q2
A — Error: lives has already been declared
Q3
A — 8
Q4
A — score = score + 1;
Q5
B — 5
Section B: Match the Pairs
Q1
score = 10; → Replaces the old value of the existing let variable score with 10, without writing let again.
Q2
score = score + 1; → Works out the old score plus 1 first, then stores that new value back in score.
Q3
To change a let variable that already exists, use its name and = only, with no let in front. → let lives = 3; lives = 2;
Q4
A new value can be built from the old value, because the right side is worked out before it is stored. → let score = 5; score = score + 2;
Q5
let score = 0; let score = 10; → let is written twice for the same name, which causes a SyntaxError because score is already declared.
Q6
let score = 4; score + 1; → score + 1 works out an answer but never stores it, so score is still 4. There is no = to reassign it.
Section C: Definitions
Q1
score = score + 1; — Reassigns an existing let variable. The right side is worked out first using the old value, then the new value replaces it. Don't write let again.
Section D: Fill in the Blanks
Q1
let score = 10; score = 20; console.log(score);
Q2
let score = 10; score = score + 1; console.log(score);
Q3
let lives = 3; lives = 2; console.log(lives);
Q4
let coins = 5; coins = coins * 2; console.log(coins);
Q5
let points = 4; points = points + 3; console.log(points);
Section E: Spot the Error
Q1
A — The declaration is missing the = sign. It should be let score = 0; so the variable starts with a value.
Q2
B — let is used again for a variable that already exists, which is an error. To reassign it, write lives = lives - 1; with no let.
Q3
D — coins was already declared on line 1, so declaring it again with let causes an error. It should be coins = coins * 2; with no let.
Q4
D — This line works out score + 1 but never stores the answer, so score does not change. It should be score = score + 1; to replace the old value.
Q5
B — The variable being updated must go on the left of =, and the new value is worked out on the right. It should be points = points + 5;