Classic JS Tutorial | JS in VS 2026 | JS Examples | jQuery
⚡ Lesson 04 of 30

Operators & Expressions

Arithmetic, comparison, logical, and assignment operators — plus operator precedence.

Arithmetic Operators

Standard math operators work as expected. The modulo % and exponentiation ** operators are especially useful:

console.log(10 + 3);   // 13
console.log(10 - 3);   // 7
console.log(10 * 3);   // 30
console.log(10 / 3);   // 3.3333...
console.log(10 % 3);   // 1  (remainder)
console.log(2 ** 8);   // 256 (2 to the power of 8)

Comparison Operators

Always prefer === (strict equality) over == (loose equality) to avoid unexpected coercion:

console.log(5 === 5);    // true
console.log(5 === "5");  // false (strict)
console.log(5 == "5");   // true  (loose — avoid!)
console.log(5 !== "5");  // true
console.log(10 > 3);     // true
console.log(10 <= 10);   // true

Logical Operators

Logical operators work on truthiness. They also short-circuit — useful for default values:

console.log(true && false);   // false
console.log(true || false);   // true
console.log(!true);           // false

// Short-circuit default value
const name = null;
const display = name || "Guest";
console.log(display); // "Guest"

Nullish Coalescing ??

The ?? operator returns the right side only if the left is null or undefined (unlike || which triggers on any falsy value):

const count = 0;
console.log(count || 10);   // 10  (0 is falsy)
console.log(count ?? 10);   // 0   (0 is not null/undefined)

Assignment & Increment

Compound assignment operators combine an operation with assignment:

let x = 10;
x += 5;   // x = 15
x -= 3;   // x = 12
x *= 2;   // x = 24
x /= 4;   // x = 6
x++;      // x = 7
x--;      // x = 6
← Lesson 03🏠 HomeLesson 05 →