Table of Contents
What the Spread Operator Does
Expands an array into individual items: the three dots unpack every value in the array.
Works inside new arrays: so you can build one array from the contents of another.
Works inside function calls: so you can pass an array's items as separate arguments.
Leaves the original array alone: nothing is changed.
let nums = [1, 2, 3]; console.log(...nums); // 1 2 3, three separate values, not an array console.log([...nums, 4, 5]); // [1, 2, 3, 4, 5]
Syntax
[...array] function(...array)
Three dots before the array name: that is all the syntax there is.
It does not work on its own: it must sit inside an array, an object or a function call.
Copying an Array
Create a separate copy: changing the copy does not affect the original.let original = [1, 2, 3]; let copy = [...original]; copy.push(4); console.log(original); // [1, 2, 3] console.log(copy); // [1, 2, 3, 4]Why not just use the equals sign: assigning an array to another variable does not copy it. Both names point to the same array.
let a = [1, 2, 3]; let b = a; b.push(4); console.log(a); // [1, 2, 3, 4], the original changed too
Combining Arrays
Join arrays together: list them one after another inside new brackets.let fruits = ["apple", "banana"]; let veggies = ["carrot", "potato"]; let food = [...fruits, ...veggies]; console.log(food); // ["apple", "banana", "carrot", "potato"]Add items anywhere: at the start, in the middle, or at the end.
let nums = [2, 3, 4]; console.log([1, ...nums, 5]); // [1, 2, 3, 4, 5] console.log([...nums, 5, 6]); // [2, 3, 4, 5, 6] console.log([0, ...nums]); // [0, 2, 3, 4]The order is up to you: the items appear in the order you write them.
Adding and Removing Items Without Changing the Original
Add an item:let tasks = ["study", "gym"]; let updated = [...tasks, "sleep"]; console.log(tasks); // ["study", "gym"] console.log(updated); // ["study", "gym", "sleep"]Remove an item by position: combine spread with slice().
let list = ["a", "b", "c", "d"]; let withoutSecond = [...list.slice(0, 1), ...list.slice(2)]; console.log(withoutSecond); // ["a", "c", "d"]Replace an item:
let letters = ["a", "b", "c"]; let replaced = [...letters.slice(0, 1), "x", ...letters.slice(2)]; console.log(replaced); // ["a", "x", "c"]This pattern matters in React: state should be replaced with a new array, not changed directly.
Passing an Array as Function Arguments
Spread the items into separate arguments: useful when a function expects individual values.let numbers = [4, 9, 2, 7]; console.log(Math.max(...numbers)); // 9 console.log(Math.min(...numbers)); // 2Without spread: Math.max(numbers) returns NaN, because it receives an array instead of numbers.
function add(a, b, c) {
return a + b + c;
}
let values = [1, 2, 3];
console.log(add(...values)); // 6
Turning Other Things Into Arrays
Strings: each character becomes an item.console.log([..."hello"]); // ["h", "e", "l", "l", "o"]Sets: useful for removing duplicates.
let nums = [1, 2, 2, 3, 3, 3]; let unique = [...new Set(nums)]; console.log(unique); // [1, 2, 3]Other iterables: such as Map keys, or the result of querySelectorAll() in the browser.
let map = new Map([["a", 1], ["b", 2]]); console.log([...map.keys()]); // ["a", "b"]
Spread vs Rest: Same Dots, Different Jobs
Spread expands: it turns one array into many values.
Rest collects: it turns many values into one array.
// Spread: expanding
let all = [...[1, 2], ...[3, 4]];
console.log(all); // [1, 2, 3, 4]
// Rest: collecting
function sum(...numbers) {
return numbers.reduce((total, n) => total + n, 0);
}
console.log(sum(1, 2, 3)); // 6
// Rest in destructuring
let [first, ...others] = [1, 2, 3, 4];
console.log(others); // [2, 3, 4]
A simple way to tell them apart: if the dots are where values are being used, it is spread. If they are where values are being received, it is rest.Things to Remember
The copy is shallow: objects inside the array are shared with the original, not duplicated.
let users = [{ name: "Vi" }];
let copy = [...users];
copy[0].name = "Changed";
console.log(users[0].name); // "Changed"
For a deep copy: use structuredClone() instead.
let deepCopy = structuredClone(users);Nested arrays are not flattened: spread only goes one level.
console.log([...[1, [2, 3]]]); // [1, [2, 3]]
Very large arrays can cause problems in function calls: spreading millions of items into arguments may exceed the engine's limit.
Holes become undefined: empty slots are filled in when spread.
Common Mistakes
Assuming it makes a deep copy: nested objects are still shared.
Forgetting the brackets: let copy = ...original is a syntax error.
Using it for an array of arrays and expecting it to flatten: use flat() instead.
Confusing it with rest: the same dots do the opposite job in function parameters.
Spreading something that is not iterable: this throws an error, so check the value first.
Frequently Asked Questions (FAQs)
2.Is the spread operator a deep copy?No. It creates a new array and leaves the original alone.
3.What is the difference between spread and rest?No. Objects inside the array are still shared, so use structuredClone() when you need a deep copy.
Spread expands an array into separate values, while rest collects separate values into an array.
0 Comments