Beginner⏱️ 8 min📘 Topic 3 of 13

🔢 JavaScript Data Types — Primitives, Objects & Type Coercion

Understand all JavaScript data types — string, number, boolean, null, undefined, symbol, bigint and objects. Learn typeof, type coercion and tricky interview gotchas.

JavaScript has 7 primitive types and one object type. That's it.

🟢 Primitives (immutable, copied by value)

  • string'hello'
  • number42, 3.14, NaN, Infinity
  • booleantrue, false
  • null — intentional "no value"
  • undefined — "not assigned yet"
  • symbol — unique identifiers (ES6)
  • bigint — huge integers (ES2020) — 9007199254740993n

🟣 Object (copied by reference)

  • Plain objects, arrays, functions, dates, regex, Maps, Sets…

🧠 Real-world analogy

Primitives are like writing a number on a slip of paper — you can copy the paper. Objects are like sharing a Google Doc link — everyone points at the same thing.

⚡ Type coercion

JavaScript silently converts types when you mix them. This is the source of half the WTFs:

  • '5' + 3'53' (number → string)
  • '5' - 32 (string → number)
  • true + 12
  • null == undefinedtrue

💡 Tip: Always use === (strict equality) — it skips coercion.

💻 Code Examples

typeof in action

typeof 'hi';        // 'string'
typeof 42;          // 'number'
typeof true;        // 'boolean'
typeof undefined;   // 'undefined'
typeof null;        // 'object'   ⚠️ historical bug
typeof [];          // 'object'
typeof function(){};// 'function'
Output:
Note: typeof null === 'object' is a 25-year-old bug that can never be fixed for backward compatibility.

Reference vs value

let a = 1, b = a; b = 99;
console.log(a); // 1 — primitives copied

let x = {n:1}, y = x; y.n = 99;
console.log(x.n); // 99 — same object
Output:
1
99

Coercion gotchas

[] + [];   // ''
[] + {};   // '[object Object]'
{} + [];   // 0 (in some contexts)
NaN === NaN; // false
Output:
Yes, JavaScript really does this.

⚠️ Common Mistakes

  • Using == instead of === and getting bitten by coercion.
  • Checking `typeof null` expecting 'null' — it returns 'object'.
  • Comparing NaN with === — it's the only value not equal to itself. Use Number.isNaN().
  • Confusing null (explicit absence) with undefined (no value assigned).

🎯 Interview Questions

Real questions asked at top product and service-based companies.

Q1.What's the difference between null and undefined?Beginner
undefined = variable declared but no value assigned (or function with no return). null = explicit 'no value here' set by the developer. typeof undefined is 'undefined'; typeof null is 'object' (a bug).
Q2.Why does typeof NaN return 'number'?Beginner
NaN literally means 'Not a Number' but it IS the number type — it represents an invalid numeric result. Use Number.isNaN(x) to detect it.
Q3.What's the difference between == and ===?Intermediate
== compares values after type coercion (so 5 == '5' is true). === compares value AND type without coercion (5 === '5' is false). Always prefer ===.
Q4.Are JavaScript strings mutable?Intermediate
No. Strings are primitives — every operation that 'changes' a string returns a new string. The original is untouched.
Q5.When would you use Symbol or BigInt?Advanced
Symbol — for unique property keys that won't collide (used in Symbol.iterator, etc.). BigInt — for integers beyond Number.MAX_SAFE_INTEGER (2^53 − 1), e.g., crypto, financial systems.

🧠 Quick Summary

  • 7 primitives + 1 object type.
  • Primitives copy by value; objects copy by reference.
  • typeof null === 'object' (historical bug).
  • NaN !== NaN — use Number.isNaN().
  • Always use === to avoid coercion surprises.