Operators
Arithmetic Operators
let a = 10, b = 3;
a + b // 13 (addition)
a - b // 7 (subtraction)
a * b // 30 (multiplication)
a / b // 3.333... (division)
a % b // 1 (modulo / remainder)
a ** b // 1000 (exponentiation, ES2016)
// Increment / Decrement
let x = 5;
x++; // post-increment: returns 5, then x becomes 6
++x; // pre-increment: x becomes 7, returns 7
x--; // post-decrement
--x; // pre-decrement
Assignment Operators
let n = 10;
n += 5; // n = n + 5 => 15
n -= 3; // n = n - 3 => 12
n *= 2; // n = n * 2 => 24
n /= 4; // n = n / 4 => 6
n %= 4; // n = n % 4 => 2
n **= 3; // n = n ** 3 => 8
n &&= 10; // logical AND assignment
n ||= 5; // logical OR assignment
n ??= 7; // nullish coalescing assignment
Comparison Operators
5 == "5" // true (loose: type coercion)
5 === "5" // false (strict: no coercion)
5 != "5" // false
5 !== "5" // true
5 > 3 // true
5 < 3 // false
5 >= 5 // true
5 <= 4 // false
Logical Operators
true && false // false (AND: both must be true)
true || false // true (OR: at least one must be true)
!true // false (NOT: negation)
// Short-circuit evaluation
let a = null;
let b = a && a.name; // b = null (doesn't access a.name)
let c = null || "default"; // c = "default"
// Logical operators return VALUES, not just booleans!
"" || "hello" // "hello"
"world" || "hello" // "world"
0 && "never" // 0
"yes" && "always" // "always"
Ternary Operator
condition ? valueIfTrue : valueIfFalse;
let age = 20;
let status = age >= 18 ? "adult" : "minor";
console.log(status); // "adult"
// Nested ternary (use sparingly)
let score = 75;
let grade = score >= 90 ? "A" : score >= 75 ? "B" : score >= 60 ? "C" : "F";
// grade = "B"
Bitwise Operators
5 & 3 // 1 (AND: 0101 & 0011 = 0001)
5 | 3 // 7 (OR: 0101 | 0011 = 0111)
5 ^ 3 // 6 (XOR: 0101 ^ 0011 = 0110)
~5 // -6 (NOT: ~00000101 = 11111010)
5 << 1 // 10 (left shift: multiply by 2)
5 >> 1 // 2 (right shift: divide by 2)
5 >>> 1 // 2 (unsigned right shift)
typeof and instanceof
typeof 42 // "number"
typeof "hello" // "string"
typeof null // "object" (quirk!)
typeof [] // "object" (arrays are objects)
[] instanceof Array // true
new Date() instanceof Date // true
// Better array check:
Array.isArray([1,2,3]) // true
Spread and Rest Operators (...)
// Spread: expands an array/object
const arr1 = [1, 2, 3];
const arr2 = [...arr1, 4, 5]; // [1, 2, 3, 4, 5]
const obj1 = { a: 1 };
const obj2 = { ...obj1, b: 2 }; // { a: 1, b: 2 }
// Rest: collects remaining items
function sum(...numbers) {
return numbers.reduce((a, b) => a + b, 0);
}
sum(1, 2, 3, 4); // 10
Operator Precedence (High → Low)
()— Grouping!,++,--,typeof— Unary**— Exponentiation*,/,%— Multiplication+,-— Addition<,>,<=,>=— Comparison==,===,!=,!==— Equality&&— Logical AND||— Logical OR??— Nullish coalescing=,+=, etc. — Assignment
MCQ — Operators
Q1: What is the output of 5 % 2?
A) 2
B) 2.5
C) 1 ✅
D) 0
Answer: C — % returns the remainder. 5 ÷ 2 = 2 remainder 1.
Q2: What is the output of let x = 5; console.log(x++);?
A) 6
B) 5 ✅
C) 4
D) undefined
Answer: B — Post-increment returns the value BEFORE incrementing.
Q3: What does "" || "hello" evaluate to?
A) true
B) false
C) ""
D) "hello" ✅
Answer: D — || returns the first truthy value. Empty string is falsy.
Q4: What is the result of 2 ** 10?
A) 20
B) 100
C) 1024 ✅
D) 512
Answer: C — ** is the exponentiation operator. 2^10 = 1024.
Q5: What does typeof [] return?
A) "array"
B) "object" ✅
C) "undefined"
D) "list"
Answer: B — Arrays are objects in JS. Use Array.isArray() to check.
Q6: What is 5 & 3?
A) 7
B) 8
C) 1 ✅
D) 15
Answer: C — Binary: 0101 & 0011 = 0001 = 1.
Q7: What does null ?? "default" return?
A) null
B) "default" ✅
C) undefined
D) false
Answer: B — ?? returns right side when left is null or undefined.
Q8: What is the output of true + true?
A) "truetrue"
B) true
C) 2 ✅
D) NaN
Answer: C — true becomes 1, so 1 + 1 = 2.