Shallow Copy Behavior
What you'll learn: You'll understand that the spread operator creates shallow copies, meaning nested objects and arrays remain referenced rather than duplicated.
The Core Idea
When you use spread (...) to copy an array, you create a shallow copy. This means the top-level array is new, but if that array contains objects or other arrays, those nested structures are not duplicated—they're still references to the original nested data.
Think of it like photocopying a folder of documents. The folder itself is a new copy, but the documents inside are still linked to the originals. If you edit a document, both folders show the change.
How Shallow Copies Work
const original = [1, 2, { name: 'Alice' }];
const copy = [...original];
console.log(copy === original); // false (different arrays)
console.log(copy[2] === original[2]); // true (same object reference!)
// Modifying the nested object affects both:
copy[2].name = 'Bob';
console.log(original[2].name); // 'Bob' (changed in original too!)
The array itself is new, but the object inside is shared. Changing primitive values (like numbers or strings) is safe, but modifying nested objects or arrays affects the original.
When This Matters
const users = [{ id: 1 }, { id: 2 }];
const backup = [...users];
backup[0].id = 999;
console.log(users[0].id); // 999 (oops!)
If you need to truly clone nested structures, spread alone won't work—you need deep cloning techniques (which we'll cover later).
Key Takeaway: Spread creates a shallow copy—the outer array is new, but nested objects and arrays remain references to the originals. Modifying nested data affects both copies.