🧩 JavaScript Deep Dive: typeof operator

🧩 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:

  • {} defines an object literal.
  • It stores data as key–value pairs.
  • So typeof {} returns "object".

🧮 2️⃣ typeof [] → "object"

const numbers = [1, 2, 3];
console.log(typeof numbers); // 👉 "object"        

✅ Explanation:

  • Arrays are special types of objects in JS used for ordered data.
  • Internally, they’re just objects with numeric keys.
  • Hence, typeof [] also returns "object".

🧩 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:

  • undefined → variable declared but not assigned any value.
  • 🧾 typeof undefined → "undefined"


  • null → explicitly assigned “no value”.
  • 🧾 typeof null → "object"

#JavaScript #WebDevelopment #LearnJavaScript #JSInterviewQuestions #Programming #Frontend #CodeNewbie

To view or add a comment, sign in

More articles by kaushal kumar

Explore content categories