const for values that never change

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

const for values that never change

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line correctly declares a value that should never change?
A const MAX_SCORE = 100;
B MAX_SCORE const = 100;
2. What happens when this code runs? const maxLives = 3; maxLives = 2;
A maxLives becomes 2 with no error
B TypeError: Assignment to constant variable
3. Which variable is best declared with const?
A The player's current score
B The number of days in a week
4. Which variable is best declared with let?
A let lives = 3; (it goes down when the player loses a life)
B const lives = 3; (it goes down when the player loses a life)
5. True or false: a const must be given its value when it is declared.
A True: const PI = 3.14;
B False: const PI; can be given a value later

Match the Pairs

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

Left columnRight column
1const MAX_SCORE = 100; AMAX_SCORE is a const, so it cannot be reassigned. This causes a TypeError: Assignment to constant variable.
2let lives = 3; BCreates a fixed value called MAX_SCORE that is set once and cannot be changed later
3Use const for a value that should never change, like the number of rounds in a game Cconst ROUNDS = 5;
4Use let for a value that will change as the program runs, like a player's score DCreates a variable that can be given a new value later, such as when a life is lost
5const MAX_SCORE = 100; MAX_SCORE = 200; Etotal needs to change, but const does not allow reassignment. It should be declared with let instead.
6const total = 0; total = total + 10; Flet score = 0;

Definitions

const

Fill in the Blanks

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

1.
_____ MAX_SCORE = 100;
2.
___ lives = 3; // this value will change
3.
const TAX = 10; console.log(5 * ___);
4.
const LIVES = 3; LIVES = 2; // _________
5.
_____ SCHOOL_YEARS = 6; // never changes

Spot the Error

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

Question 1
A const MAX_SCORE = 100;
B let score = 90;
C score += 15;
D MAX_SCORE = score;
Question 2
A const PI = 3.14;
B const radius = 5;
C radius = radius * 2;
D console.log(PI * radius);
Question 3
A const TAX = 0.1;
B let price = 50;
C const total;
D console.log(total);
Question 4
A const MAX_LIVES = 3;
B let lives = MAX_LIVES;
C MAX_LIVES--;
D console.log(lives);
Question 5
A cosnt MAX_PLAYERS = 4;
B let joined = 2;
C console.log(MAX_PLAYERS - joined);

Answer Key

const for values that never change
Section A: Quick Questions
Q1
A — const MAX_SCORE = 100;
Q2
B — TypeError: Assignment to constant variable
Q3
B — The number of days in a week
Q4
A — let lives = 3; (it goes down when the player loses a life)
Q5
A — True: const PI = 3.14;
Section B: Match the Pairs
Q1
const MAX_SCORE = 100; → Creates a fixed value called MAX_SCORE that is set once and cannot be changed later
Q2
let lives = 3; → Creates a variable that can be given a new value later, such as when a life is lost
Q3
Use const for a value that should never change, like the number of rounds in a game → const ROUNDS = 5;
Q4
Use let for a value that will change as the program runs, like a player's score → let score = 0;
Q5
const MAX_SCORE = 100; MAX_SCORE = 200; → MAX_SCORE is a const, so it cannot be reassigned. This causes a TypeError: Assignment to constant variable.
Q6
const total = 0; total = total + 10; → total needs to change, but const does not allow reassignment. It should be declared with let instead.
Section C: Definitions
Q1
const — Declares a variable whose value is set once and cannot be reassigned. Use it for fixed values like a maximum score. Changing it later causes a TypeError.
Section D: Fill in the Blanks
Q1
const MAX_SCORE = 100;
Q2
let lives = 3; // this value will change
Q3
const TAX = 10; console.log(5 * TAX);
Q4
const LIVES = 3; LIVES = 2; // TypeError
Q5
const SCHOOL_YEARS = 6; // never changes
Section E: Spot the Error
Q1
D — MAX_SCORE was declared with const, so it cannot be reassigned. This causes a TypeError. The line should cap the score: score = MAX_SCORE;
Q2
B — radius changes on the next line, so it must be declared with let. With const, radius = radius * 2 would cause a TypeError. It should be let radius = 5;
Q3
C — A const must be given its value when it is declared, because it can never be assigned later. It should be const total = price + price * TAX;
Q4
C — MAX_LIVES is a const, so it cannot be changed with --. This causes a TypeError. The changing variable lives should be reduced: lives--;
Q5
A — The keyword const is misspelled as cosnt. JavaScript does not recognise it, so it is a syntax error. It should be const MAX_PLAYERS = 4;