Skip to content
BasicsChapter 7 / 47Beginner4 min read

Operators

A guide to JavaScript operators, arithmetic, assignment, comparison, logical and nullish operators, with short and clear examples.

On this page

1. What are operators?

In 2 + 3, + is the operator and 2 and 3 are the operands. The main groups of operators:

GroupOperators
Arithmetic+, -, *, /, %, **, ++, --
Assignment=, +=, -=, *=, ??=, ||=, &&=
Comparison===, !==, >, <, >=, <=
Logical&&, ||, !
Nullish??
Otherstypeof, ternary ? :, optional chaining ?.

2. Why does it matter?

Every program runs on calculations and decisions. Working out a total, applying a discount, checking a login, all of it is done with operators. Choosing the right operator makes code shorter and less buggy.

The long wayWith the right operator
count = count + 1count += 1 or count++
Every condition in a separate ifTwo conditions in one line with and
A full if else for a defaultA default in one line with the nullish operator
Math.pow for a squareThe exponent operator with two stars

3. Where is it used?

Real useOperator
Cart total+, *, +=
Even or odd check%
Checking both login and admin&&
Default username??
Age 18 or above>=
Exact match===

4. How does it work?

When an expression has several operators, precedence decides which runs first, just like school math.

Simple order of precedence
Brackets ( )
Exponent **
Multiply, divide, remainder
Plus, minus
Comparison
Logical and, then or

Logical operators short circuit. && stops at the first falsy value. || stops at the first truthy value. ?? takes the right side only on null or undefined.

5. Code example

Arithmetic and assignment:

js
const price = 500;
const qty = 3;
let total = price * qty;

total += 100;
console.log(total);
console.log(10 % 3);
console.log(2 ** 5);

Output: 1600, 1, 32.

Comparison and logical:

js
const age = 20;
const hasTicket = true;

console.log(age >= 18 && hasTicket);
console.log(age < 18 || hasTicket);
console.log(!hasTicket);

Output: true, true, false.

The difference between || and ??:

js
const savedVolume = 0;

const v1 = savedVolume || 50;
const v2 = savedVolume ?? 50;
console.log(v1, v2);

const settings = {};
settings.theme ??= "dark";
console.log(settings.theme);

Output: 50 0, then dark. The user had set the volume to 0, || wrongly changed it to 50, and ?? correctly kept 0.

How does the code work?

ItemMeaning
total += 100Short form of total = total + 100
10 % 3Remainder, 1
2 ** 52 to the power 5, 32
&&True if both are true
||True if either one is true
??The right side if the left side is null / undefined
??=Assign a value only if it is empty

Interview tip

Mixing ?? with && or || without brackets throws a SyntaxError, like a || b ?? c. Brackets are required: (a || b) ?? c.

6. Common mistakes

Mistake: Using || for a default when 0 or an empty string is valid

Why? || replaces every falsy value (0, empty string, false), so valid data is lost.

Better: Use ?? when you want a default only for null and undefined.

Mistake: Using == for comparison

Why? == does type coercion, so even 0 == empty string comes out true.

Better: Always use === and !==.

Mistake: Guessing the precedence

Why? In long expressions it is unclear which part runs first, and you get a wrong result.

Better: Add brackets wherever you are unsure. The code also becomes easier to read.

7. What to learn before and after

Final quick revision

PointDetail
Arithmetic+ - * / % **
Shortcut assignment+=, -=, ??=
ComparisonAlways === and !==
Logical&&, ||, ! short circuit
??A default only on null / undefined
PrecedenceUse brackets when in doubt