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);
⏰ Preorder my new course on Web Components! Want to learn how to build Web Components from scratch, master best practices, and more? Preorder today and get $100 off of the launch price.
Find this useful? You can support my work by purchasing an annual membership.