Arithmetic Operations in JavaScript
JavaScript arithmetic operators + - * / % ** and ++/--. Learn why ^ is not power and how to divide whole numbers, with a simple explanation and output.
1let a = 10;2let b = 3;34// Addition5let sum = a + b;6console.log(`${a} + ${b} = ${sum}`);78// Subtraction9let difference = a - b;10console.log(`${a} - ${b} = ${difference}`);1112// Multiplication13let product = a * b;14console.log(`${a} * ${b} = ${product}`);1516// Division17let quotient = a / b;18console.log(`${a} / ${b} = ${quotient}`);1920// Modulus (Remainder)21let remainder = a % b;22console.log(`${a} % ${b} = ${remainder}`);2324// Exponentiation (ES6)25let power = a ** b;26console.log(`${a} ** ${b} = ${power}`);2728// Increment29let x = 5;30x++; // Post-increment31console.log("After x++:", x);3233let y = 5;34++y; // Pre-increment35console.log("After ++y:", y);3637// Decrement38let p = 5;39p--; // Post-decrement40console.log("After p--:", p);4142let q = 5;43--q; // Pre-decrement44console.log("After --q:", q);
Sample Output
10 / 3 = 3.3333333333333335
The final 5 is a rounding effect of binary decimals. It is normal.
After x++: 6
x++ on its own line simply adds 1, same as ++y.
In this one
- All arithmetic operators in JavaScript
- Why / returns decimals and how to get whole numbers
- What % (remainder) is used for
- Why ** is power and ^ is not
- The difference between x++ and ++x
How it works
- 1
a is 10 and b is 3.
- 2
Each operator result is stored and printed with a template literal.
- 3
Division prints a long decimal.
- 4
x++ and ++y both change 5 into 6.
- 5
p-- and --q both change 5 into 4.
Edge cases
Using ^ for power
2 ^ 3 returns 1.
^ is bitwise XOR. Use 2 ** 3 or Math.pow(2, 3) to get 8.
Division by zero
10 / 0 returns Infinity, not an error.
JavaScript does not throw here. Check the divisor yourself if zero is not allowed.
Remainder of negative numbers
-7 % 3 returns -1.
The sign follows the left number. Keep this in mind for even or odd checks.
What's going on
This program uses a = 10 and b = 3 to show every arithmetic operator. An operator is a symbol that does work on values, like + or *.
Addition, subtraction and multiplication work like school math. Division is different from some other languages. In JavaScript / always gives a decimal result, so 10 / 3 is 3.3333333333333335. The long tail comes from how computers store decimals. If you want whole number division, use Math.trunc(10 / 3), which gives 3.
The % operator gives the remainder. 10 % 3 is 1 because 3 goes into 10 three times with 1 left over. You will use % a lot, for example to check even or odd numbers.
The
operator means power. 10
3 is 1000. Do not use ^ for power. In JavaScript ^ is XOR, a bit operation, so 2 ^ 3 gives 1, not 8. This catches many students who come from math class.
The last part shows increment and decrement. x++ adds 1 and ++y also adds 1. When used alone on a line they do the same thing. The difference appears inside an expression. x++ gives back the old value and then adds. ++y adds first and gives back the new value. The same rule applies to -- for subtracting 1.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What is 17 % 5?
Pick an answer to check it.
Question 2
What is 2 ** 4?
Pick an answer to check it.
Questions people ask
Why is 10 / 3 printed with so many digits?
Decimals are stored in binary and cannot be exact. Use toFixed(2) to show two decimal places.
How do I get integer division?
Use Math.trunc(a / b) or Math.floor(a / b) for positive numbers.
Is x++ faster than x = x + 1?
No real difference. Choose the one that reads best.