Operators act on **operands** (values). JavaScript operators cover arithmetic, comparison (always prefer `===` over `==`), logical (`&&`, `||`, `??`), assignment, bitwise, and the ternary operator. The **nullish coalescing** `??` returns the right side only when the left is `null` or `undefined`.
1Understanding Operators
Operators act on operands (values). JavaScript operators cover arithmetic, comparison (always prefer === over ==), logical (&&, ||, ??), assignment, bitwise, and the ternary operator. The nullish coalescing ?? returns the right side only when the left is null or undefined.
Always use === (strict equality) instead of == (loose equality) to avoid unexpected type coercion.
// Arithmetic operators
console.log(10 + 3); // 13
console.log(10 - 3); // 7
console.log(10 * 3); // 30
console.log(10 / 3); // 3.333...
console.log(10 % 3); // 1 (remainder)
console.log(2 ** 8); // 256 (exponent)2Practical Example
Here is a real-world application of Operators showing how it is used in production JavaScript code.
// Nullish coalescing vs logical OR
const count = 0;
console.log(count || 'default'); // 'default' (wrong!)
console.log(count ?? 'default'); // 0 (correct)
// Ternary
const age = 20;
const label = age >= 18 ? 'adult' : 'minor';
console.log(label); // 'adult'3Best Practices
Follow these guidelines when working with Operators:
1. Use === and !== for comparisons
2. Use ?? instead of || when 0 or '' are valid values
3. Use ** instead of Math.pow()
Tip: Always use === (strict equality) instead of == (loose equality) to avoid unexpected type coercion.
// Arithmetic operators
console.log(10 + 3); // 13
console.log(10 - 3); // 7
console.log(10 * 3); // 30
console.log(10 / 3); // 3.333...
console.log(10 % 3); // 1 (remainder)
console.log(2 ** 8); // 256 (exponent)