Primitive vs Non-Primitive Data Types
Primitive vs Non-Primitive Data Types
Everything in JavaScript is either a Primitive (a simple value like a number or word) or an Object (a collection of data). Understanding how they are copied is the foundation of all JavaScript code.
The Core Idea: The Two Data Families
JavaScript organizes all values into two distinct categories:
The 7 Primitives
Primitives are single, simple pieces of data. When you copy a primitive, JavaScript makes a completely independent duplicate.
Objects (Reference Types)
Objects hold collections of data or logic. When you assign an object to another variable, you only copy a pointer; both variables share the same object.
┌────────────────────────────────────────────────────────┐
│ JAVASCRIPT DATA TYPES │
└───────────────────────────┬────────────────────────────┘
│
┌───────────────┴───────────────┐
▼ ▼
┌───────────────────────┐ ┌───────────────────────┐
│ 7 PRIMITIVES │ │ 1 REFERENCE TYPE │
│ (Copied by Value) │ │ (Copied by Reference) │
├───────────────────────┤ ├───────────────────────┤
│ • String "hello" │ │ • Object { a: 1 } │
│ • Number 42, 3.14 │ │ • Array [1, 2, 3] │
│ • Boolean true │ │ • Function () => {} │
│ • Undefined undefined │ │ │
│ • Null null │ │ (Collections of data │
│ • BigInt 99n │ │ and properties) │
│ • Symbol Symbol() │ │ │
└───────────────────────┘ └───────────────────────┘How They Work: Value vs Reference in 2 Examples
Here is the essential difference every programmer must remember:
let a = 10;
let b = a; // b receives its own copy of 10
b = 20; // only b is modified
console.log(a); // 10 (Safe & unchanged!)
console.log(b); // 20Takeaway: Variable b holds an independent copy. Changing b never touches a.
let user1 = { name: "Alice" };
let user2 = user1; // Both point to the SAME object!
user2.name = "Bob"; // Mutates shared object!
console.log(user1.name); // "Bob" (Changed!)
console.log(user2.name); // "Bob"Takeaway: Variable user2 does not get a copy of the object; it points to the exact same object in memory.
The Classic Mistake: Accidental Mutation & The Fix
When beginners want to copy an object without modifying the original, they often write b = a by mistake. Here is how to fix it:
const original = { role: "admin" };
const copy = original; // NOT a new object
copy.role = "guest";
console.log(original.role); // "guest" (Corrupted!)Assigning objects copies only the memory pointer, not the contents.
const original = { role: "admin" };
const copy = { ...original }; // Brand-new copy!
copy.role = "guest";
console.log(original.role); // "admin" (Safe!)Use object spread { ...original } to create a fresh, independent object.
Checking Types with typeof
JavaScript provides the built-in typeof operator to inspect the data type of any variable:
| Expression | Result | Explanation |
|---|---|---|
| typeof "Alice" | "string" | Text surrounded by quotes |
| typeof 42 | "number" | Numbers and decimals (e.g. 3.14) |
| typeof true | "boolean" | true or false truth values |
| typeof undefined | "undefined" | Declared variable with no value assigned |
| typeof { id: 1 } | "object" | Key-value pair object collection |
| typeof [1, 2, 3] | "object" | Arrays are objects in JavaScript! |
| typeof null | "object" | A famous 1995 JS quirk (null is a primitive, but returns object) |
Quick Knowledge Check
Test your understanding on these 3 simple questions (one by one):
What is the core purpose of Primitive vs Non-Primitive Data Types in JavaScript?
// Concept 8: Primitive vs Non-Primitive Data Types
// Understanding the core mental modelSelect an answer: