Operators

Symbols that perform actions on values, like math or comparisons.

What is it?

You often need to do things with values: add two numbers, check which is bigger, or combine two conditions into one decision. Operators are the symbols that do this. Each takes one or more values and produces a result.

The groups you'll use daily:

  • Arithmetic: + - * / % for math
  • Comparison: === !== > < >= <= for comparing two values
  • Logical: && || ! for combining true/false conditions
  • Assignment: = += -= for storing or updating a value

Explain like I'm 10

Operators are like calculator buttons: each one does a specific, predictable action with the numbers you've entered.

Examples

Arithmetic and comparison

const total = 10 + 5;      // 15
const isEqual = 10 === 10; // true
const isBigger = 10 > 20;  // false

Logical operators combining conditions

const age = 20;
const hasTicket = true;

const canEnter = age >= 18 && hasTicket;
console.log(canEnter); // true

const skipLine = age < 12 || age > 65;
console.log(skipLine); // false

&& needs both sides to be true; || needs only one. Both combine several conditions into one decision.

How it works

Each operator takes one or two values (operands) and produces a result. + turns two numbers into their sum. === turns two values into a boolean: true if they match exactly, false otherwise.

Why does it exist?

Programs constantly calculate, compare and combine values to make decisions. Operators are the building blocks for that logic.

When to use it

Use arithmetic operators to calculate and comparison operators to decide. Use logical operators (&&, ||) when a decision depends on more than one condition.

When not to use it

Avoid == in new code: its type conversions cause more bugs than they prevent, so make === your default. And if a condition chains too many logical operators to read at a glance, move it into a well-named variable.

Common mistakes

  • Using = (assignment) when you meant === (comparison).

  • Using == instead of === — == converts types before comparing, which can cause surprising results like "5" == 5 being true.

  • Forgetting operator precedence, e.g. assuming 2 + 3 * 4 equals 20 instead of 14.

Practice exercises

  1. Easy:

    Write an expression that adds two numbers and multiplies the result by 2.

  2. Medium:

    Use === to compare two values of different types and explain the result.

  3. Hard:

    Write a one-line expression using && and || that checks if a user is either an admin or (a member and over 18).

Interview questions

What's the difference between `==` and `===`?

== (loose equality) converts differing types to a common type before comparing, which gives surprises like "5" == 5. === (strict equality) compares value and type with no conversion, so different types are never equal. It's the safer default almost always.

What does the `%` (modulo) operator do?

It returns the remainder after division: 10 % 3 is 1, since 3 goes into 10 three times with 1 left over. It's the standard way to check divisibility (n % 2 === 0 for even) or wrap a number into a fixed range.

What's the difference between `=`, `==`, and `===`?

= is assignment: it stores a value in a variable. == and === are comparisons that produce a boolean; == converts types first, === doesn't. Writing = in a condition by mistake is a classic bug: if (x = 5) assigns 5 to x and the condition is truthy.

What do `&&` and `||` do?

&& (AND) is truthy only if both operands are truthy; || (OR) needs at least one. Both combine conditions into one true/false decision.

What does the `!` operator do?

Logical NOT: it converts its operand to a boolean and flips it, so !true is false and !0 is true (0 is falsy). !!value is a common shorthand to turn any value into its boolean equivalent.

What is short-circuit evaluation?

JavaScript stops evaluating a logical expression once the result is decided. In a || b, if a is truthy the result is already truthy, so b is never evaluated.

Why does `a || b` sometimes not evaluate `b` at all?

|| needs only one truthy operand. Once a is truthy, b can't change the answer, so it's skipped. This matters if b is a function call with side effects: they won't happen.

If both operands of `&&` are truthy, what does the expression actually evaluate to?

Not true. If the first operand is truthy, && returns the second operand's value, so "a" && "b" is "b". It only looks boolean when the operands are booleans.

If the first operand of `||` is truthy, what does the expression evaluate to?

That operand's own value, not true. || returns the first truthy operand, so "hello" || "fallback" is "hello". Default-value patterns like const name = input || "Guest"; rely on this.

What is operator precedence, and how does it explain the result of `2 + 3 * 4`?

Precedence is the fixed order JavaScript evaluates mixed operators in, regardless of left-to-right order. Multiplication binds tighter than addition, so 3 * 4 (12) comes first, then 2 + 12 gives 14, not 20.

What's the difference between `x += 1` and `x = x + 1`?

Nothing in practice. += is shorthand: it reads x, adds 1 and stores the result back in x, exactly like the longer form.

What is the nullish coalescing operator (`??`), and how does it differ from `||`?

a ?? b gives b only when a is null or undefined. Other falsy values like 0, "" or false are returned as-is. || falls back for any falsy a, which wrongly overrides valid values like 0 or an empty string.

What is optional chaining (`?.`), and what problem does it solve?

It returns undefined instead of throwing when a value partway down a chain is null or undefined. user?.address?.city safely gives undefined if user or address is missing, instead of throwing TypeError: Cannot read properties of undefined.

What's the difference between `++x` (prefix) and `x++` (postfix)?

Both add 1 to x, but the expression's value differs: ++x increments first and gives the new value; x++ gives the old value, then increments. This only matters inside a larger expression: arr[i++] indexes with the current i, then increments it.

Why do `&&` and `||` return one of their actual operand values instead of a plain boolean?

It's deliberate, and it enables useful patterns: default values (input || "default"), guarded property access (user && user.name) and conditional calls (isReady && doThing()). All rely on getting a real value back, not just true/false.

What does `NaN == NaN` evaluate to? What about `NaN === NaN`?

Both are false. Under the IEEE 754 spec, NaN never equals any value, including itself, whichever equality operator you use.

Why does `0.1 + 0.2 === 0.3` evaluate to `false`?

JavaScript numbers are IEEE 754 double-precision floats, which can't store most decimal fractions exactly in binary. 0.1 + 0.2 is actually about 0.30000000000000004, not exactly 0.3. Exact equality on floats is unreliable in every language that uses this format, not just JavaScript.

Output-prediction: what does `console.log(1 < 2 < 3)` log?

true, but not for the obvious reason. < runs left to right, so it's (1 < 2) < 3: 1 < 2 is true, then true < 3 turns true into 1, and 1 < 3 is true. The same pattern gives a misleading true for 3 < 2 < 1, since it becomes false < 1 → 0 < 1.

Output-prediction: what do `console.log("5" == 5)` and `console.log("5" === 5)` log?

true then false. == converts "5" to the number 5 first, so they match. === never converts types, and a string is never strictly equal to a number.

Output-prediction: what do `console.log(null ?? "default")` and `console.log(0 ?? "default")` log?

"default" then 0. ?? only falls back for null or undefined. 0 isn't nullish, so it's returned even though it's falsy.

Output-prediction: what does `let x = 5; console.log(x++ + ++x);` log?

It logs 12. x++ gives 5 (then x becomes 6); ++x makes x 7 and gives 7. So it's 5 + 7 = 12, and x ends up 7.