Skip to content
Concept 08 // Module 01: JavaScript Foundations & Environment

Primitive vs Non-Primitive Data Types

Module 01: JavaScript Foundations & Environment•Concept 08 of 100• 5 min read

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:

Family 1 • Copied by Value7 Types

The 7 Primitives

Primitives are single, simple pieces of data. When you copy a primitive, JavaScript makes a completely independent duplicate.

• string → "hello"
• number → 42, 3.14
• boolean → true, false
• undefined → undefined
• null → null
• bigint → 9007199254740991n
• symbol → Symbol("id")
Family 2 • Copied by Reference1 Type

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.

• Object → { name: "Alice" }
• Array → [1, 2, 3]
• Function → () => { ... }
(In JavaScript, Arrays and Functions are specialized kinds of Objects!)
How Data Is Organized:
┌────────────────────────────────────────────────────────┐
│                  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:

Example 1 • Primitives Make Independent Copies
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); // 20

Takeaway: Variable b holds an independent copy. Changing b never touches a.

Example 2 • Objects Share the Same Reference
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:

The Mistake (Shared Reference)
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.

The Fix (Spread Operator { ... })
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:

ExpressionResultExplanation
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):

Score: 0 / 10
Question 1 of 10 • Core Purpose & Mental Model

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 model

Select an answer:

Live Memory Studio

Primitives create independent copies on the stack: changing scoreB never affects scoreA.
JavaScript Editor
Loading...
Console Output
Waiting for output...