What is the spread operator (...) in JavaScript — and why the order of three dots matters

Here are two lines. Spot the difference:
const a = { ...user, name: "Anna" }
const b = { name: "Anna", ...user }
Only the order differs. But the result doesn't match: in a the name is definitely "Anna", while in b it can get overwritten by the old name from user. Three dots in code aren't an ellipsis — they're an operator. Let's see how it works, and this trap will become obvious.
What spread is, in plain words
The spread operator is three dots ... in front of an array or object. They pour the contents out, like a sack turned upside down.
The sack itself disappears — you're left with the items laid out one by one. And you can put them straight somewhere new: a new array, a new object, or a function call.
const fruits = ["apple", "pear"]
const basket = [...fruits, "banana"]
// basket = ["apple", "pear", "banana"]
Without the three dots you'd get [["apple", "pear"], "banana"] — the whole sack inside the basket. With them, just the fruit.
What it does with arrays
Arrays are where AI puts ... most often. Three main moves.
Copy. const copy = [...fruits] — a new array with the same items. Change the copy and the original stays put.
Merge. const all = [...fruits, ...vegetables] — two lists into one, in the order written.
Pass items to a function one by one. Some functions want numbers separated by commas, not an array. Math.max, for example:
const prices = [300, 900, 150]
Math.max(prices) // NaN — it was handed the sack
Math.max(...prices) // 900 — the numbers were poured out
Bonus: a string can be poured out too. [..."cat"] gives ["c", "a", "t"] — an array of letters.
What it does with objects
With objects, the three dots pour out field–value pairs. The main move is copy and change one field:
const user = { name: "Anna", age: 25 }
const older = { ...user, age: 26 }
// { name: "Anna", age: 26 }, and user is unchanged
This is where the rule from the start kicks in. Fields are laid out left to right. If a name repeats, the last one wins.
{ ...user, age: 26 }— old fields first, then the newage. New overwrites old. This is how you write updates.{ age: 26, ...user }—age: 26first, thenuserpours out with its ownage: 25. Old overwrites new. This is how you write defaults.
Remember one phrase: rightmost wins.
In React this is the main way to change state. You can't edit it in place, you have to create a new object. setUser({ ...user, age: 26 }) does exactly that.
The trap: the copy is only one level deep
Spread copies one level. If an object holds another object inside, the copy gets a reference to that very same one.
const user = { name: "Anna", address: { city: "Almaty" } }
const copy = { ...user }
copy.address.city = "Astana"
console.log(user.address.city) // "Astana" — the original changed too!
copy is a new object, but address inside it is shared with the original. It's like copying a folder that contains a shortcut: the shortcut is new, the file it points to is the same.
The fix: spread the nested level separately:
const copy = { ...user, address: { ...user.address, city: "Astana" } }
Or, if you need a full deep copy, there's a built-in function: structuredClone(user).
Same three dots, reversed: rest
Sometimes ... sits on the left or in a function's parameters. Then it does the opposite: instead of pouring out, it gathers the rest into a sack. That's called rest.
function sum(...numbers) {
// numbers is an array of everything passed in
}
sum(1, 2, 3) // numbers = [1, 2, 3]
const { password, ...safeUser } = user
// safeUser — everything except password
The second move is handy: drop a field from an object before sending it to a server. The rule is simple: to the right of = or in a call — it pours out; on the left or in parameters — it gathers.
Are spread and destructuring the same thing?
No, but they work together. Destructuring takes an object apart into variables, and rest inside it gathers whatever you didn't pick out.
Is spread fast?
For lists of hundreds or thousands of items — yes, don't worry about it. Copying huge arrays over and over in a loop gets expensive, but beginners rarely hit that.
Does Python have spread?
Yes, it just looks different: * for lists and ** for dictionaries. For example, {**user, "age": 26} does the same as { ...user, age: 26 }.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





