TypeError: assignment to a constant variable

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

TypeError: assignment to a constant variable

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A student writes const score = 0; then score = 10; What happens when the code runs?
A TypeError: Assignment to constant variable
B score becomes 10 with no error
2. Which declaration lets you change the value later without an error?
A let lives = 3;
B const lives = 3;
3. The console shows: TypeError: Assignment to constant variable. What is the cause?
A The code tried to reassign a const variable
B The code forgot a semicolon at the end of a line
4. Which code will cause a TypeError?
A let total = 5; total = total + 1;
B const total = 5; total = total + 1;
5. How do you fix this code? const count = 0; count++;
A Change count++ to count--
B Change const to let

Match the Pairs

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

Left columnRight column
1TypeError: Assignment to constant variable. Alet lives = 3; lives = 2;
2let score = 0; score = 5; Bname holds a string, not a function, so calling it with () causes a TypeError. Only call names that are functions.
3Reassigning a const variable causes a TypeError Cconst lives = 3; lives = 2;
4Fix the error by changing const to let when the value must change DA variable declared with let can be given a new value, so no TypeError happens.
5const points = 10; points += 5; Epoints is a const, so += tries to give it a new value and causes a TypeError. Declare it with let instead.
6const name = "Sam"; name(); FThe error JavaScript shows when code tries to give a new value to a const variable.

Definitions

TypeError: Assignment to constant variable

Fill in the Blanks

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

1.
___ score = 0; score = score + 5;
2.
const lives = 3; lives = 2; // Error type: _________
3.
const name = "Sam"; name(); // TypeError: ____ is not a function
4.
const lives = 3; lives = 2; // TypeError: Assignment to ________ variable
5.
_____ PI = 3.14159; console.log(PI);

Spot the Error

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

Question 1
A const name = "Ava";
B console.log(name);
C const score = 0;
D score = score + 10;
Question 2
A const MAX_LIVES = 3;
B const lives = MAX_LIVES;
C lives--;
D console.log(lives);
Question 3
A let count = 0;
B const limit = 3;
C while (count < limit) {
D limit += 1;
E }
Question 4
A const price = 5;
B price = price + 2;
C console.log(price);
Question 5
A const age = 12;
B console.log(age());
C console.log(age + 1);

Answer Key

TypeError: assignment to a constant variable
Section A: Quick Questions
Q1
A — TypeError: Assignment to constant variable
Q2
A — let lives = 3;
Q3
A — The code tried to reassign a const variable
Q4
B — const total = 5; total = total + 1;
Q5
B — Change const to let
Section B: Match the Pairs
Q1
TypeError: Assignment to constant variable. → The error JavaScript shows when code tries to give a new value to a const variable.
Q2
let score = 0; score = 5; → A variable declared with let can be given a new value, so no TypeError happens.
Q3
Reassigning a const variable causes a TypeError → const lives = 3; lives = 2;
Q4
Fix the error by changing const to let when the value must change → let lives = 3; lives = 2;
Q5
const points = 10; points += 5; → points is a const, so += tries to give it a new value and causes a TypeError. Declare it with let instead.
Q6
const name = "Sam"; name(); → name holds a string, not a function, so calling it with () causes a TypeError. Only call names that are functions.
Section C: Definitions
Q1
TypeError: Assignment to constant variable — The error JavaScript shows when code tries to give a new value to a const variable. Fix it by declaring the variable with let if its value needs to change.
Section D: Fill in the Blanks
Q1
let score = 0; score = score + 5;
Q2
const lives = 3; lives = 2; // Error type: TypeError
Q3
const name = "Sam"; name(); // TypeError: name is not a function
Q4
const lives = 3; lives = 2; // TypeError: Assignment to constant variable
Q5
const PI = 3.14159; console.log(PI);
Section E: Spot the Error
Q1
C — score is given a new value on line 4, so it cannot be a const. Reassigning it gives TypeError: Assignment to constant variable. Declare it with let score = 0; instead.
Q2
B — lives-- changes the value, but a const cannot change. This causes TypeError: Assignment to constant variable. Use let lives = MAX_LIVES; because lives changes.
Q3
D — limit is a const, so adding to it causes TypeError: Assignment to constant variable. The loop should update the counter that changes, count += 1;
Q4
A — price is changed on line 2, so a const will throw TypeError: Assignment to constant variable. Use let price = 5; for values that change.
Q5
B — age holds a number, not a function, so the brackets () cannot call it. JavaScript shows TypeError: age is not a function. It should be console.log(age);