Calculating on the Fly
JavaScript can do everything your scientific calculator can.
1. Basic Operations
+,-,*,/.%(Remainder):5 % 2 = 1. Useful for checking parity (even/odd).**(Exponentiation):2 ** 3 = 8.
2. String Addition (Concatenation)
The + operator is special. If at least one operand is a string, the result will be a string.
alert(2 + 2 + '1'); // "41" (first 2+2=4, then 4 + '1' = "41")
alert('1' + 2 + 2); // "122" (immediately turned into strings)
Important: Other operators (-, *, /) always turn everything into numbers!
3. Shorthand Assignment
let n = 2;
n += 5; // n = n + 5 (now 7)
n *= 2; // n = n * 2 (now 14)
4. Increment / Decrement
counter++— increase by 1.counter--— decrease by 1.
Important
Precedence: First everything in parentheses, then multiplication/division, then addition/subtraction. And assignment (=) has the lowest priority.