Operators and strings · Lesson 5 of 45
Arithmetic and assignment
Add, subtract, remainder and powers, plus += and ++.
7 minutes3 games
The basic operators are +, -, * and /. Two more are important:
%remainder:17 % 5is2. Very handy:n % 2 === 0means n is even.**power:2 ** 10is1024.
Precedence follows maths: multiply and divide before add and subtract, brackets before everything.
JavaScript
console.log(17 % 5) // 2 console.log(2 ** 10) // 1024 console.log(2 + 3 * 4) // 14 console.log((2 + 3) * 4) // 20 console.log(7 / 2) // 3.5 console.log(Math.floor(7 / 2)) // 3
Assignment shortcuts
Step by step1 / 5
- ▶let likes = 9
- 2likes += 1
- 3likes *= 2
- 4likes++
- 5console.log(likes)
Variables
| likes | 9 |
Output
…
What's the output?
console.log(10 % 3) console.log(2 + 2 * 2)
What will this code print?
Complete the code
Add price to total using the shortcut.
let total = 100 const price = 25 total price
Exercise
Print how many minutes are in 3 hours and 20 minutes (200).
Expected output: 200
JavaScript
const hours = 3 const minutes = 20
Learn more from
Played 0 of 3 games
Sign in to save your progress and earn XP.