Assignment = versus equality ===

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

Assignment = versus equality ===

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line stores the value 10 in the variable score?
A score = 10;
B score === 10;
2. Which expression checks whether score is equal to 10?
A score === 10
B score = 10
3. How should you read this line aloud: lives = 3;
A lives is equal to 3
B lives becomes 3
4. How should you read this condition aloud: age === 13
A age is equal to 13
B age becomes 13
5. Which if statement is written correctly to check if x is 5?
A if (x = 5) {
B if (x === 5) {

Match the Pairs

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

Left columnRight column
1let score = 10; AA single = assigns 5 to x instead of comparing. The condition should use === to ask 'is x equal to 5?'
2score === 10 BStores the value 10 in the variable score (score becomes 10)
3Read = as 'becomes' when you store a value in a variable CChecks if score is equal to 10 and gives true or false
4Use === inside an if to ask 'is it equal to?' Dif (lives === 0) {
5if (x = 5) { Elet lives = 3;
6let age === 13; F=== compares values, so it cannot store 13 in age. A single = is needed to assign the value.

Definitions

= vs ===

Fill in the Blanks

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

1.
let score _ 10;
2.
let score = 10; if (score ___ 10) console.log("Win");
3.
let lives = 0; console.log(lives ___ 0);
4.
let isReady = false; isReady _ true; console.log(isReady);
5.
let n = 7; if (n ___ 7) { console.log("Lucky"); }

Spot the Error

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

Question 1
A let score = 10;
B let target = 10;
C console.log("Checking");
D if (score = target) {
E console.log("Match!");
F }
Question 2
A let lives = 3;
B console.log("Start");
C if (lives = 0) {
D console.log("Game over");
E }
Question 3
A let x = 2;
B let y = 3;
C console.log(x);
D x === y + 1;
E console.log(x);
Question 4
A let password = "abc";
B let guess = "abc";
C if (guess = password) {
D console.log("Welcome");
E }
Question 5
A let lives;
B lives === 3;
C console.log(lives);

Answer Key

Assignment = versus equality ===
Section A: Quick Questions
Q1
A — score = 10;
Q2
A — score === 10
Q3
B — lives becomes 3
Q4
A — age is equal to 13
Q5
B — if (x === 5) {
Section B: Match the Pairs
Q1
let score = 10; → Stores the value 10 in the variable score (score becomes 10)
Q2
score === 10 → Checks if score is equal to 10 and gives true or false
Q3
Read = as 'becomes' when you store a value in a variable → let lives = 3;
Q4
Use === inside an if to ask 'is it equal to?' → if (lives === 0) {
Q5
if (x = 5) { → A single = assigns 5 to x instead of comparing. The condition should use === to ask 'is x equal to 5?'
Q6
let age === 13; → === compares values, so it cannot store 13 in age. A single = is needed to assign the value.
Section C: Definitions
Q1
= vs === — A single = stores a value in a variable (x becomes 5). Triple === compares two values and gives true or false (x is equal to 5). Writing if (x = 5) is a bug.
Section D: Fill in the Blanks
Q1
let score = 10;
Q2
let score = 10; if (score === 10) console.log("Win");
Q3
let lives = 0; console.log(lives === 0);
Q4
let isReady = false; isReady = true; console.log(isReady);
Q5
let n = 7; if (n === 7) { console.log("Lucky"); }
Section E: Spot the Error
Q1
D — A single = stores a value, so this sets score to target instead of comparing them. To ask 'is equal to' you need ===: if (score === target) {
Q2
C — if (lives = 0) stores 0 in lives instead of checking it. The condition should compare with ===: if (lives === 0) {
Q3
D — This line should store a new value in x, so it needs a single = (x becomes y + 1). === only compares and would leave x unchanged. Correct line: x = y + 1;
Q4
C — A single = overwrites guess with password, so the check is not a real comparison. Use === to ask if they are equal: if (guess === password) {
Q5
B — To give lives a value you read = as 'becomes': lives = 3;. === only compares two values and does not store anything.