← All lessons/Variables & Data Types· Lesson 6 of 32

Numbers & arithmetic

Do math with + - * / and %, and reassign a variable to update its value.

About 7 min

Math with numbers

Numbers are the most common kind of value. You can combine them with the usual math operators and store the result in a variable:

  • + add, - subtract
  • * multiply, / divide
  • % remainder — what's left after dividing (e.g. 7 % 3 is 1)
Computing values and printing them.

Reassigning

A variable declared with let can be reassigned — just use = again, without let. The box keeps its name but holds a new value:

let score = 5;
score = score + 3; // score is now 8
Updating a variable in place.

NOTE

score = score + 3 looks odd, but the right side is computed first (5 + 3), then the answer is stored back into score.

Your turn

At the top level, declare a variable named result and use arithmetic to make it equal 20. For example, store 8 + 12 (or any math that adds up to 20) in result.

Hints