Numbers and Maths

Doing arithmetic with operators

9 min read · runnable Javascript

JavaScript is great with numbers. Unlike strings, numbers do not get quotes around them. You just write them plainly.

let apples = 5;
let price = 1.5;

Numbers can be whole (like 5) or have a decimal point (like 1.5). JavaScript treats them all the same way.

The maths operators

An operator is a symbol that does something to values. Here are the main maths ones:

  • + add
  • - subtract
  • * multiply (the star, not the letter x)
  • / divide
  • % remainder (what is left over after dividing)
console.log(10 + 3);  // 13
console.log(10 - 3);  // 7
console.log(10 * 3);  // 30
console.log(10 / 3);  // 3.333...
console.log(10 % 3);  // 1  (10 divided by 3 leaves 1 left over)

Maths with variables

You can do maths with variables just like with plain numbers:

let apples = 5;
let pricePerApple = 2;
let total = apples * pricePerApple;
console.log(total); // 10
Tip: Watch out! + means add for numbers, but join for strings. 2 + 2 is 4, but "2" + "2" is "22". The quotes make all the difference.

Try it yourself

Change the numbers and try each operator to see what you get.

Try it yourself
Output
Press “Run code” to see the result.

Quick check

Q1 Which operator gives the remainder after division?

Q2 What does console.log("3" + "4") print?

Want to save your progress? It's free.

Create a free account