Operators & Expressions

The building blocks of computation. Combine values, compare data, and control logic.

Arithmetic Operators

Basic math operations on numbers.

Interactive — change values

and

Addition

10 + 3 = 13

Subtraction

10 - 3 = 7

Multiplication

10 * 3 = 30

Division

10 / 3 = 3.3333

Modulus

10 % 3 = 1

Exponent

10 ** 3 = 1000

// Increment & Decrement
let count = 5;
count++;    // 6 (post-increment)
count--;    // 5 (post-decrement)
++count;    // 6 (pre-increment)

// Unary operators
let x = "5";
console.log(+x);   // 5 (converts to number)
console.log(-x);   // -5

💡 String + Number: "5" + 3 = "53" (concatenation, not addition). Use Number() or + to convert first.

Assignment Operators

Store and update values in variables.

OperatorExampleSame As
=x = 10x = 10
+=x += 5x = x + 5
-=x -= 3x = x - 3
*=x *= 2x = x * 2
/=x /= 4x = x / 4
%=x %= 3x = x % 3
**=x **= 2x = x ** 2
let score = 0;
console.log(score);

Start with score = 0.

💡 score = 0
1 / 5

Comparison Operators

Compare values and get true or false.

ExpressionResultWhy
5 == "5"trueLoose: converts types
5 === "5"falseStrict: different types
null == undefinedtrueSpecial case in spec
null === undefinedfalseDifferent types
NaN === NaNfalseNaN is never equal to anything

🏆 Always use === (strict equality). It checks both value and type, avoiding confusing type coercion.

Logical Operators

Combine conditions with AND, OR, and NOT.

Interactive — toggle values

A && Bfalse
A || Btrue
!Afalse
// Short-circuit evaluation
let user = null;
let name = user && user.name;  // null (stops at falsy)
let fallback = name || "Guest"; // "Guest" (finds first truthy)

// Nullish coalescing (see next section)
let count = 0;
let result = count ?? 10; // 0 (only null/undefined trigger ??)

Falsy Values in JavaScript

false0-0""nullundefinedNaN

Everything else is truthy (including "0", "false", [], {}).

Ternary & Nullish Coalescing

Concise conditional expressions.

// Ternary operator: condition ? ifTrue : ifFalse
let age = 20;
let status = age >= 18 ? "adult" : "minor";
console.log(status);

The ternary operator is a one-line if/else. If the condition is true, return the first value; otherwise the second.

💡 status = "adult"
1 / 5

Operator Precedence

Which operations execute first?

PriorityOperatorExample
1 (highest)( )(2 + 3) * 4
2** ++ -- !2**3, !true
3* / %6 * 2 / 3
4+ -5 + 3 - 1
5< > <= >=a > b
6== != === !==a === b
7&&a && b
8||a || b
9??a ?? b
10 (lowest)= += -= etc.x = 5
// What does this evaluate to?
let result = 2 + 3 * 4 ** 2;
// Step 1: 4 ** 2 = 16
// Step 2: 3 * 16 = 48
// Step 3: 2 + 48 = 50
console.log(result);
50

🏆 Best practice: Use parentheses to make complex expressions readable, even when not strictly necessary.

Exercises

Test your understanding.

Try it yourself
1.

What does 10 % 3 return?

2.

What does this output?

console.log("5" == 5);
3.

What does this output?

console.log("5" === 5);
4.

What does true && 0 || 'hello' evaluate to?

Frequently Asked Questions

Common questions about operators.