🧩 JavaScript Deep Dive: typeof operator
Let’s understand one of the most commonly used (and sometimes confusing) JavaScript operators — typeof 🧠
🔍 Examples
console.log(typeof "Mohan"); 👉 "string"
console.log(typeof 25); 👉 "number"
console.log(typeof true); 👉 "boolean"
console.log(typeof null); 👉 "object" ❗
console.log(typeof undefined); 👉 "undefined"
console.log(typeof [1,2,3]); 👉 "object"
console.log(typeof function(){}); 👉 "function"
🤷♂️ Pro Tip
typeof null === "object" is actually a historical bug in JavaScript — and it’s kept for backward compatibility 🧩
🧱 1️⃣ typeof {} → "object"
const person = { name: "Aarav", age: 25 };
console.log(typeof person); // 👉 "object"
✅ Explanation:
🧮 2️⃣ typeof [] → "object"
const numbers = [1, 2, 3];
console.log(typeof numbers); // 👉 "object"
✅ Explanation:
🧩 How to Differentiate Between Object and Array
Since both {} and [] return "object", use Array.isArray() 👇
console.log(Array.isArray([])); // true
console.log(Array.isArray({})); // false
🧠 Summary Table
Expression :- {}
Type :- "object"
How to Check :- use typeof
Expression :- []
Type :- "object"
How to Check :- use Array.isArray()
Expression :- function(){}
Type :- "function"
How to Check :- callable object type
💬 Interview Question
❓ What’s the difference between undefined and null?
✅ Answer:
#JavaScript #WebDevelopment #LearnJavaScript #JSInterviewQuestions #Programming #Frontend #CodeNewbie