Course contentsShow
← JavaScript
Lesson 42 of 2,7232. Values and TypesFree lesson

Primitives Are Copied by Value

Learn how primitive values are copied when assigned or passed, creating independent copies.

Primitives Are Copied by Value

What you'll learn: How JavaScript creates independent copies when you assign or pass primitive values.

The Core Idea

When you assign a primitive value to a new variable or pass it to a function, JavaScript copies the actual value into the new location. The two variables hold completely separate values—changing one never affects the other.

Think of it like photocopying a document. If you write on the photocopy, the original paper stays unchanged. They're independent pieces of paper, even though they started with the same content.

How It Works

let x = 5;
let y = x;  // y gets a COPY of the value 5

y = 10;     // changing y doesn't affect x

console.log(x);  // 5 (unchanged)
console.log(y);  // 10

Even though y = x happened, these variables are now independent. y received its own copy of 5, not a connection to x.

Why "By Value" Matters

This behavior applies to all seven primitive types you've learned: undefined, null, boolean, number, string, symbol, and bigint.

let name = "Alice";
let greeting = name;  // greeting gets a copy of "Alice"

greeting = "Bob";     // doesn't change name

console.log(name);     // "Alice" (still)
console.log(greeting); // "Bob"

Because primitives are also immutable (as you learned earlier), you can't accidentally modify the original value through the copy—it's a true, independent duplicate.

Key Takeaway: Primitive values are copied when assigned or passed, creating independent copies that don't affect each other.