Beginner LevelJavaScript & TypeScript
What are the JavaScript data types and the difference between primitives and objects?
❓ Interview Prompt: List all JavaScript data types. Explain primitive vs reference types and how typeof works.
JavaScript has 8 data types divided into Primitives and Objects:
7 Primitive Types (immutable, stored by value):
1
string — text ("hello")2
number — integers and floats (42, 3.14, NaN, Infinity)3
bigint — arbitrary precision integers (9007199254740993n)4
boolean — true / false5
undefined — declared but unassigned variable6
null — intentional absence of value7
symbol — unique, immutable identifier (Symbol('id'))1 Reference Type (mutable, stored by reference):
8
object — includes plain objects, arrays, functions, Date, RegExp, Map, Set, etc.typeof Quirk:
typeof null returns "object" — this is a legacy JavaScript bug from 1995 that cannot be fixed for backward compatibility.
Code Implementation Example
javascript
// Primitives (compared by value)
const a = 'hello';
const b = 'hello';
console.log(a === b); // true
// Objects (compared by reference)
const obj1 = { x: 1 };
const obj2 = { x: 1 };
console.log(obj1 === obj2); // false (different references)
// typeof checks
typeof 42; // "number"
typeof 'hello'; // "string"
typeof true; // "boolean"
typeof undefined; // "undefined"
typeof null; // "object" (legacy bug!)
typeof Symbol('x'); // "symbol"
typeof {}; // "object"
typeof []; // "object" (use Array.isArray())💡Primitive value comparison vs object reference comparison and typeof quirks.
🎯 Interview Key Takeaway
Primitives are compared by value, objects by reference. Use Array.isArray() instead of typeof to check arrays.
#Data Types#Primitives#Objects#Beginner
Back to all JavaScript & TypeScript Questions→