Operators
=== (strict equality) value AUR type dono match hone chahiye. == (loose equality) sirf value compare karta hai, type coerce (convert) kar deta hai. Modern JavaScript mein HAMESHA === use karna best practice hai — == unpredictable "type coercion" bugs deta hai.
Logical: && (AND), || (OR), ! (NOT). ?? (nullish coalescing, ES2020) — sirf null/undefined ke liye fallback deta hai (|| ke ulat, jo koi bhi "falsy" value — 0, "", false — ke liye bhi fallback de deta hai, jo kabhi-kabhi unwanted hota hai).
console.log(5 === "5"); // false — type match nahi (number vs string)
console.log(5 == "5"); // true — loose, type coerce ho jaata hai
let count = 0;
console.log(count || 10); // 10 — 0 "falsy" hai, || fallback de deta hai (SHAYAD unwanted!)
console.log(count ?? 10); // 0 — ?? sirf null/undefined ke liye fallback deta hai
let a = true, b = false;
console.log(a && b); // false
console.log(a || b); // true- === (strict) hamesha use karo, == (loose) avoid karo
- &&/||/! = logical operators
- ?? = sirf null/undefined ke liye fallback (|| se safer)
?. (optional chaining, ES2020) safely nested properties access karne deta hai bina manual null-checks ke — agar beech mein koi property null/undefined hai, poora expression undefined return karta hai (error throw nahi karta).
const user = { profile: { name: "Aarav" } };
console.log(user.profile?.name); // "Aarav"
console.log(user.address?.city); // undefined (error nahi, safely handled)
// Bina optional chaining ke (purana, verbose tareeka):
// console.log(user.address && user.address.city);++/-- C/Java jaise hi hain — prefix (++x, pehle increment phir use) aur postfix (x++, pehle use phir increment) mein subtle farq hota hai jab expression ke andar use karo.
let x = 5;
console.log(x++); // 5 (prints OLD value, phir increment)
console.log(x); // 6
let y = 5;
console.log(++y); // 6 (pehle increment, phir prints NEW value)