JavaScript Tutorial
JavaScript Arithmetic
Add, subtract, multiply, divide, remainder, and increment. Watch the order of operations.
The arithmetic set
Arithmetic operators work on numbers. JavaScript has one number type for both integers and fractions, so10 / 4 is 2.5. You do not switch to a separate float type.
| Operator | Name | Example | Result |
|---|---|---|---|
+ | add | 7 + 2 | 9 |
- | subtract | 7 - 2 | 5 |
* | multiply | 7 * 2 | 14 |
/ | divide | 7 / 2 | 3.5 |
% | remainder | 7 % 2 | 1 |
** | exponent | 2 ** 3 | 8 |
Add, subtract, multiply, divide
Write the numbers, the operator, and read the result with console.log. Store a result in a name when you need it again.
Example
const width = 12;
const height = 5;
const area = width * height;
const perimeter = 2 * (width + height);
console.log(area);
console.log(perimeter);
console.log(width / height);
Run this in /javascript/try. The console under the page prints the numbers. That editor is.
Remainder
% is not percent. It is the remainder after division. 10 % 3 is 1.10 % 2 is 0 — that is how you test even numbers. 10 % 10 is0. 3 % 10 is 3 because 10 does not fit into 3 even once.
Example
console.log(10 % 3);
console.log(10 % 2);
console.log(3 % 10);
const n = 14;
if (n % 2 === 0) {
console.log(n + " is even");
} else {
console.log(n + " is odd");
}
Remainder is also how a counter wraps. If you have 5 slides and the index is i, the next index is (i + 1) % 5. After 4 comes 0.
Increment and decrement
++ adds one. -- subtracts one. Written as its own statement, n++ and++n both change n by one. This tutorial uses them on their own line so prefix and postfix do not matter yet.
After let n = 5;, the statement n++; leaves n equal to 6.n--; would take it back to 5. You can also write n += 1 if you want the change to look like assignment.
Example
let count = 0;
count++;
count++;
console.log(count);
count--;
console.log(count);
Do not write const count = 0; count++;. const cannot be reassigned. Increment needs let.
Order of operations
Exponent runs first, then multiply, divide, and remainder, then add and subtract. Same-level operators run left to right. Parentheses always win. When a formula is a real calculation — tax, a total, a remaining budget — put parentheses around the groups you mean, even if the default order already matches.
Example
console.log(2 + 3 * 4);
console.log((2 + 3) * 4);
console.log(2 ** 3 * 2);
console.log(10 - 4 - 3);
console.log(10 - (4 - 3));
2 + 3 * 4 is 14. (2 + 3) * 4 is 20.2 ** 3 * 2 is 16 because the power runs first. 10 - 4 - 3 is3 (left to right). 10 - (4 - 3) is 9.
Unary plus and minus
A minus in front of a value negates it: -n flips the sign. A plus in front of a string tries to turn that string into a number: +"42" is 42. If the string is not numeric,+"hello" is NaN (Not a Number). Prefer Number("42") when you convert on purpose — it reads as a conversion, not as arithmetic.
Mixing strings and numbers
+ concatenates if either side is a string. The other arithmetic operators coerce strings to numbers instead. "10" - 3 is 7. "10" * 3 is 30."10" + 3 is "103". That split is why totals go wrong when a form value is still text.
Form fields give you strings. Convert before you add: Number(input.value) orparseFloat(input.value). Then + adds.
Next: assignment — putting a value in a name, and the shortcuts that change the value already there.