Operators

Topic 1 of 5 in Operators & control flow8 min readBeginnerFree course

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:

JavaScript
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:

JavaScript
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:

JavaScript
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 6

When ++ 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:

JavaScript
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;      // true

Always 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:

JavaScript
[1, 2] === [1, 2];   // false
const list = [1, 2];
list === list;       // true — the same array

Range 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:

JavaScript
const x = 50;
1 < x < 10;          // true!? (1 < 50 is true, and true < 10 is true)
x > 1 && x < 10;     // false — correct

Logical 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).

A ladder of operator precedence from first to last: grouping, member access and calls, unary, exponent, multiply and divide, add and subtract, comparison, equality, AND, OR and nullish, the conditional operator, and assignment. Beside it, 2 + 3 * 4 > 10 && ok is worked out step by step: the multiply first, then the add, then the comparison, then && returns ok.

JavaScript
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) - 3

You 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:

JavaScript
const age = 20;
const label = age >= 18 ? 'adult' : 'minor';

It’s perfect inside template literals and other places that need a value:

JavaScript
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:

JavaScript
// Hard to read
const size = n > 100 ? 'large' : n > 10 ? 'medium' : 'small';

typeof

typeof returns a string naming a value’s type:

JavaScript
typeof 42;          // 'number'
typeof 'hi';        // 'string'
typeof {};          // 'object'
typeof (() => {});  // 'function'
typeof undeclared;  // 'undefined' — no error, even for unknown names

That last line makes typeof handy for checking whether a browser feature exists: if (typeof structuredClone === 'function') { … }.

Other operators you’ll meet

  • in checks whether an object has a property: 'name' in user.
  • instanceof checks whether an object was made by a class: err instanceof TypeError.
  • delete removes a property from an object: delete user.password.
  • , (comma) evaluates several expressions and returns the last. It mostly appears in for loops: for (let i = 0, j = 10; i < j; i++, j--).
  • Spread ..., optional chaining ?. and nullish coalescing ?? each get their own topic later.
Exercise

Predict the values

Work out the final value of score and label before running the code.

JavaScript
let score = 10;
score += 5;
score *= 2;
score--;
const label = score > 25 ? 'high' : 'low';
console.log(score, label);

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 : b chooses between two values.