⚙️ Special Operators in JavaScript
Last Updated: 11th October 2025
JavaScript provides some special operators that make code safer and cleaner when dealing with null values, object properties, or type checks.
These operators are:
??→ Nullish Coalescing?.→ Optional Chainingtypeof→ Type Checkinginstanceof→ Object Instance Checking...→ Spread Operator
🗣 Hinglish Tip: Ye operator code ko crash hone se bachate hain aur type ya object ko samajhne me help karte hain.
🧩 Nullish Coalescing (??)
Used to give a default value only when a variable is null or undefined.
Syntax:
let result = value ?? defaultValue;
Example:
let username = null;
let displayName = username ?? "Guest";
console.log(displayName); // Output: Guest
Hinglish Tip 🗣: Nullish Coalescing ka matlab hai ek default value ko access karna, jaise ki null ho, ya undefined ho.
Note Difference from OR (||):
|| checks for falsy values (0, "", false, etc.), while ?? only checks for null or undefined.
let count = 0;
console.log(count || 5); // Output: 5 (because 0 is falsy)
console.log(count ?? 5); // Output: 0 (because 0 is NOT null/undefined)
🧭 Optional Chaining (?.)
Safely access nested object properties without error if something is undefined.
Syntax:
object?.property;
object?.[key];
object?.method?.();
Example:
let user = {
profile: { name: "Rahul" },
};
console.log(user.profile?.name); // Rahul
console.log(user.address?.city); // undefined (no error)
Hinglish Tip 🗣: Agar property exist nahi karti, to JS bas undefined return karega — error nahi aayega.
📜 Type Checking (typeof)
Used to check the data type of a value or variable.
Syntax:
typeof variable;
Example:
console.log(typeof 42); // number
console.log(typeof "Hello"); // string
console.log(typeof true); // boolean
console.log(typeof undefined); // undefined
console.log(typeof null); // object (special case!)
console.log(typeof {}); // object
console.log(typeof []); // object
💡 Note: typeof null gives "object" (this is a long-standing quirk in JS).
🔎 Object Instance Checking (instanceof)
Used to check if an object is an instance of a specific class or constructor.
Syntax:
object instanceof constructor;
Example:
let arr = [1, 2, 3];
console.log(arr instanceof Array); // true
console.log(arr instanceof Object); // true
console.log(arr instanceof String); // false
function Student() {}
let s1 = new Student();
console.log(s1 instanceof Student); // true
🌈 Spread Operator (...)
The spread operator (...) is used to expand (spread) elements of an array, object, or iterable into individual items.
Hinglish Tip 🗣: “Spread” ka matlab hota hai — cheezon ko faila dena ya khol dena.
🧩 In Arrays
It copies or merges arrays easily:
const nums1 = [1, 2, 3];
const nums2 = [4, 5];
const all = [...nums1, ...nums2];
console.log(all); // [1, 2, 3, 4, 5]
🧱 In Objects
You can copy or combine objects too:
const user = { name: "Aarav", age: 20 };
const extra = { city: "Delhi" };
const newUser = { ...user, ...extra };
console.log(newUser);
// { name: "Aarav", age: 20, city: "Delhi" }
Don't worry now we will cover in upcoming sections.Just Remember this.