The arithmetic operators
% keeps the sign of the left number and works on decimals too. Dividing by zero gives Infinity or NaN, never an error.
Operators are the symbols that combine values into new values: + - * / % ** for maths, = and += to store results, ++ and -- to count, < > <= >= to compare, and + again to join strings, all evaluated in a fixed order called precedence.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 5 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write a line that prints whether the number n = 17 is even or odd using %.
A shop sells pens at 12 rupees. Read the quantity "4" (a string, as from an input), and print Total: 48.
Without running it, predict what console.log(3 + 4 * 2 ** 2 - 1) prints, then check.
Explain it without notes
Why does "5" + 2 give "52" while "5" - 2 gives 3?
What is the difference between x++ and ++x?
What is operator precedence, and how do you control it?
Why is "10" < "9" true but "10" < 9 false?
Preview
Console
Nothing logged yet.Expected console output
7 + 2 = 9
7 - 2 = 5
7 * 2 = 14
7 / 2 = 3.5
7 % 2 = 1
2 ** 3 = 8
-7 % 2 = -1
5.5 % 2 = 1.5
1 / 0 = Infinity
0 / 0 = NaN