Shorthand updates: +=, -= and ++

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

Shorthand updates: +=, -= and ++

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line does the same job as total = total + price;?
A total += price;
B total =+ price;
2. What does count++; do to the variable count?
A Adds 2 to count
B Adds 1 to count
3. Which statement takes 1 away from lives?
A lives--;
B lives-;
4. let score = 10; score += 5; What is score now?
A 15
B 5
5. Which is the same as x = x + 1;?
A x+1;
B x++;

Match the Pairs

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

Left columnRight column
1total += price; Acount+ is not a valid operator. To add 1 you need two plus signs, count++, or you can write count += 1.
2count--; Bcount++;
3Add 1 to the variable count using the shortest form CThe symbols are the wrong way round. =+ just assigns a positive price to total instead of adding it. It should be +=.
4Write score = score - 5 in its shorthand form Dscore -= 5;
5total =+ price; EAdds the value of price to total and stores the answer back in total
6let count = 0; count+; FTakes 1 away from count

Definitions

total += price

Fill in the Blanks

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

1.
let total = 0; total __ 5; console.log(total);
2.
let lives = 3; lives__; console.log(lives);
3.
let count = 0; count = _____ + 1; count++;
4.
const price = 8; let money = 20; money __ price;
5.
const price = 6; let total = 0; _____ += price;

Spot the Error

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

Question 1
A const count = 0;
B count++;
C console.log(count);
Question 2
A let total = 10;
B total =+ 5;
C console.log(total);
Question 3
A let score = 0;
B score += 10;
C score+;
D console.log(score);
Question 4
A let total;
B total += 4;
C total += 6;
D console.log(total);
Question 5
A let lives = 3;
B lives =- 1;
C console.log(lives);

Answer Key

Shorthand updates: +=, -= and ++
Section A: Quick Questions
Q1
A — total += price;
Q2
B — Adds 1 to count
Q3
A — lives--;
Q4
A — 15
Q5
B — x++;
Section B: Match the Pairs
Q1
total += price; → Adds the value of price to total and stores the answer back in total
Q2
count--; → Takes 1 away from count
Q3
Add 1 to the variable count using the shortest form → count++;
Q4
Write score = score - 5 in its shorthand form → score -= 5;
Q5
total =+ price; → The symbols are the wrong way round. =+ just assigns a positive price to total instead of adding it. It should be +=.
Q6
let count = 0; count+; → count+ is not a valid operator. To add 1 you need two plus signs, count++, or you can write count += 1.
Section C: Definitions
Q1
total += price — A shorthand update. Operators like +=, -=, ++ and -- change a variable using its own value, so total += price means total = total + price. count++ adds 1 and count-- takes away 1.
Section D: Fill in the Blanks
Q1
let total = 0; total += 5; console.log(total);
Q2
let lives = 3; lives--; console.log(lives);
Q3
let count = 0; count = count + 1; count++;
Q4
const price = 8; let money = 20; money -= price;
Q5
const price = 6; let total = 0; total += price;
Section E: Spot the Error
Q1
A — A const variable cannot be changed, so count++ would cause a TypeError. Use let count = 0; for a variable that gets updated.
Q2
B — The operator is += (plus first, then equals). =+ just assigns positive 5, so total becomes 5 instead of 15. It should be total += 5;
Q3
C — The increase operator needs two plus signs. A single + is a syntax error. It should be score++;
Q4
A — Without a starting value, total is undefined, and undefined + 4 gives NaN. A running total must start at 0: let total = 0;
Q5
B — The operator is -= (minus first, then equals). =- assigns -1, so lives becomes -1 instead of 2. It should be lives -= 1;