Operators
Overview
Operators combine values to produce new ones. The values an operator works on are called its operands: in 3 + 4, the operator is + and the operands are 3 and 4.
Operators come in three sizes:
- Unary operators take one operand:
!isDone,-x,typeof value,count++. - Binary operators take two:
a + b,a === b,a && b. - The ternary operator takes three:
condition ? yes : no.
You’ve already met the arithmetic operators in Numbers and math. This topic covers the others you’ll use constantly, and how JavaScript decides the order to apply them.
Assignment
The plain = stores a value in a variable. It’s not the same as “equals” in maths — read x = 5 as “x becomes 5”.
Compound assignment operators update a variable using its current value:
let count = 1;
count += 2; // count = count + 2 → 3
count -= 1; // 2
count *= 5; // 10
count /= 2; // 5
count %= 3; // 2
count **= 3; // 8
count++; // 9
count--; // 8+= also works on strings, which is handy for building text:
let html = '<ul>';
html += '<li>One</li>';
html += '</ul>';Prefix and postfix increment
++ and -- come in two forms. Both change the variable, but they produce different values when used inside a bigger expression:
let a = 5;
const b = a++; // postfix: b gets the OLD value (5), then a becomes 6
let c = 5;
const d = ++c; // prefix: c becomes 6 first, then d gets 6When ++ stands alone on its own line, the two forms behave identically. Avoid using it inside larger expressions — it’s easy to misread.
Comparison
Comparisons produce true or false:
5 > 3; // true
5 >= 5; // true
3 < 2; // false
3 <= 3; // true
'a' < 'b'; // true (compares character codes)
5 === 5; // true
5 === '5'; // false — different types
5 !== 6; // trueAlways prefer
===. The loose==converts types first, which is a common source of bugs. See Type coercion.
Remember that objects and arrays are compared by reference, so two separate objects are never ===, even if they look identical:
[1, 2] === [1, 2]; // false
const list = [1, 2];
list === list; // true — the same arrayRange checks
You can’t chain comparisons like in maths. 1 < x < 10 runs, but it compares true/false with 10 and gives nonsense. Combine two comparisons with && instead:
const x = 50;
1 < x < 10; // true!? (1 < 50 is true, and true < 10 is true)
x > 1 && x < 10; // false — correctLogical operators at a glance
You’ll learn these in depth in Logical and nullish operators, but they appear in many examples before then:
| Operator | Meaning | Example |
|---|---|---|
&& |
AND — both must be true | age >= 18 && hasTicket |
|| |
OR — at least one is true | isAdmin || isOwner |
! |
NOT — flips true and false | !isLoggedIn |
?? |
fallback for null/undefined |
name ?? 'Guest' |
Precedence
When an expression has several operators, precedence decides which runs first. Like in maths, * and / happen before + and -. When two operators have the same precedence, most run left to right (** and assignment run right to left).
2 + 3 * 4; // 14
(2 + 3) * 4; // 20
2 ** 3 ** 2; // 512 — right to left: 2 ** 9
10 - 4 - 3; // 3 — left to right: (10 - 4) - 3You don’t need to memorise the whole table. Remember the broad order — maths, then comparisons, then logic, then assignment — and use brackets whenever the order isn’t obvious. Brackets cost nothing and make your intent clear to the next reader.
The ternary operator
A compact if/else that produces a value. It reads as condition ? value if true : value if false:
const age = 20;
const label = age >= 18 ? 'adult' : 'minor';It’s perfect inside template literals and other places that need a value:
console.log(`You have ${count} message${count === 1 ? '' : 's'}`);Ternaries can be nested, but that quickly becomes hard to read. Once there are more than two outcomes, use if…else or a lookup object instead:
// Hard to read
const size = n > 100 ? 'large' : n > 10 ? 'medium' : 'small';typeof
typeof returns a string naming a value’s type:
typeof 42; // 'number'
typeof 'hi'; // 'string'
typeof {}; // 'object'
typeof (() => {}); // 'function'
typeof undeclared; // 'undefined' — no error, even for unknown namesThat last line makes typeof handy for checking whether a browser feature exists: if (typeof structuredClone === 'function') { … }.
Other operators you’ll meet
inchecks whether an object has a property:'name' in user.instanceofchecks whether an object was made by a class:err instanceof TypeError.deleteremoves a property from an object:delete user.password.,(comma) evaluates several expressions and returns the last. It mostly appears inforloops:for (let i = 0, j = 10; i < j; i++, j--).- Spread
..., optional chaining?.and nullish coalescing??each get their own topic later.
Predict the values
Work out the final value of score and label before running the code.
let score = 10;
score += 5;
score *= 2;
score--;
const label = score > 25 ? 'high' : 'low';
console.log(score, label);// 10 → 15 → 30 → 29, and 29 > 25 is true
console.log(29, 'high');Summary
- An operator takes one or more values (operands) and produces a new value.
- Compound assignment (
+=,*=) updates a variable in place;++/--add or subtract one. - Comparisons produce booleans. Use
===and!==. - Operator precedence decides the order — add brackets when in doubt.
- The ternary
condition ? a : bchooses between two values.