AnyCompiler

Numbers and Basic Math Operators

Unlike Python, JavaScript has just one number type — number — for both whole numbers and decimals. Most arithmetic operators work exactly like a calculator.

let a = 10;
let b = 3;

console.log(a + b);
console.log(a - b);
console.log(a * b);
console.log(a / b);
console.log(a % b);
console.log(a ** b);
Output
13
7
30
3.3333333333333335
1
1000
  • + - * / work exactly like you'd expect
  • / is regular division — there's no separate floor-division operator like Python's //
  • % is the modulo operator — gives you the remainder after division
  • ** is exponentiation — a ** b means a to the power of b

Flooring a division result

Since there's no // operator, use Math.floor() to round a division result down to the nearest whole number.

console.log(Math.floor(17 / 5));
console.log(Math.round(4.6));
console.log(Math.ceil(4.2));
Output
3
5
5

Math.floor() always rounds down, Math.round() rounds to the nearest whole number, and Math.ceil() always rounds up — the same three-way split as Python's // , round(), and math.ceil(), just spelled differently.

Next: strings — how text works in JavaScript, and template literals for building text from variables.

Practice exercise

Given a = 17 and b = 5, print Math.floor(a / b), then print a % b.