NelsonLabs
JavaScript Basics/Operators & Expressions

Operators & Expressions

Operators are symbols that perform operations on values. You already know the maths ones — JavaScript adds a few more that are critical to understand.

Arithmetic operators
javascript
const a = 10;
const b = 3;

a + b   // 13  addition
a - b   // 7   subtraction
a * b   // 30  multiplication
a / b   // 3.333... division
a % b   // 1   remainder (modulo) — very useful for "is X even/odd?"
a ** b  // 1000 exponentiation (10 to the power of 3)

// Increment and decrement
let count = 0;
count++;   // count is now 1
count--;   // count is now 0
count += 5; // count is now 5
count *= 2; // count is now 10
Comparison operators — always return true or false
javascript
// Strict equality (checks value AND type) — always use ===
5 === 5    // true
5 === "5"  // false (different types)
5 !== "5"  // true

// Loose equality (converts types before comparing) — avoid ==
5 == "5"   // true  (coerces "5" to number)
0 == false // true  (this is a bug waiting to happen)

// Order comparisons
10 > 5    // true
10 < 5    // false
10 >= 10  // true
10 <= 9   // false
Logical operators
javascript
// AND — true only if BOTH are true
true && true   // true
true && false  // false

// OR — true if AT LEAST ONE is true
true  || false  // true
false || false  // false

// NOT — flips the boolean
!true   // false
!false  // true

// Short-circuit evaluation — very common in React
const user = null;
const name = user && user.name;  // null (short-circuits — user is falsy)

const displayName = user?.name || "Guest";  // "Guest" if user is null

// Nullish coalescing — only falls back on null or undefined
const port = process.env.PORT ?? 3000;