Lesson 11 +10 XP

JavaScript Arithmetic

JavaScript Arithmetic

Arithmetic operators perform math on numbers.

The basic operators

OperatorDescriptionExampleResult
+Addition5 + 27
-Subtraction5 - 23
*Multiplication5 * 210
/Division5 / 22.5
%Modulus (remainder)5 % 21
**Exponentiation5 ** 225

Increment and decrement

let x = 5;
x++; // x becomes 6
x--; // x becomes 5

Precedence

Like in math class, JavaScript follows order of operations. Multiplication and division happen before addition and subtraction:

let result = 2 + 3 * 4; // 14, not 20

Use parentheses to control the order:

let result = (2 + 3) * 4; // 20

The + on strings

The + operator also joins strings:

"Hello" + " " + "World" // "Hello World"

TL;DR

  • Arithmetic: + - / % *.
  • ++ adds one, -- subtracts one.
  • Multiplication and division come before addition.
  • Parentheses control the order.
  • + joins strings too.