Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 2.1 — Operators
JavaScript · Example 1 of 4

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.

01

Write a line that prints whether the number n = 17 is even or odd using %.

02

A shop sells pens at 12 rupees. Read the quantity "4" (a string, as from an input), and print Total: 48.

03

Without running it, predict what console.log(3 + 4 * 2 ** 2 - 1) prints, then check.

Explain it without notes

01

Why does "5" + 2 give "52" while "5" - 2 gives 3?

02

What is the difference between x++ and ++x?

03

What is operator precedence, and how do you control it?

04

Why is "10" < "9" true but "10" < 9 false?

The arithmetic operators

Preview

Press Run to see the page.

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