Basic math
JavaScript includes a handful of basic arithmetic operators:
Operator | Action |
---|---|
+ | Add |
- | Subtract |
* | Multiply |
/ | Divide |
% | Remainder |
// Add
// logs 4
console.log(2 + 2);
// Subtract
// logs 3
console.log(4 - 1);
// Divide
// logs 2
console.log(4 / 2);
// Multiply
// logs 10
console.log(5 * 2);
// Remainder
// logs 1 (2 goes into 5 twice, with 1 left over)
console.log(5 % 2);
You can combine operators. Group the items you want to run first/together with parentheses.
let total = (3 + 1) * 2;
// logs 8 (3 plus 1 is 4, multiplied by 2)
console.log(total);