Spread and Rest in JavaScript: arrays, objects, and the shallow copy gotchas | jonathanjuliani.dev
Technical·May 16, 2026 · 12 min
Spread and Rest in JavaScript: arrays, objects, and the shallow copy gotchas
Spread operator and rest in JavaScript: how they work in arrays, objects, and functions, and the shallow copy gotchas that catch every developer.
Jonathan Juliani
You do const copy = [...original], modify the copy, and the original array changes with it. Or you clone an object with spread and discover a nested property is still a shared reference. Spread looks simpler than it is — and the bugs it hides are the hardest to debug because they throw no exception.
Spread and rest use the same syntax (...) for opposite purposes: spread expands an iterable into its elements, rest collects separate elements into an array. Understanding when each one kicks in — and where shallow copy gets you — is what this post covers.
What you'll learn:
Spread in arrays: clone, combine, and spread as function arguments
Spread in objects: replacing Object.assign for good
const result = Object.assign(defaults, preferences);// defaults was mutated — result and defaults are the same object
With spread, you always get a new object. The accidental mutation of Object.assign is why object spread became the standard. If you want to use Object.assign without mutating, you pass an empty object as the first argument: Object.assign({}, a, b) — but at that point, spread is more readable.
Rest parameters: spread in reverse
Rest parameters collect the remaining arguments of a function call into a single array. It's ... on the receiving side, not the calling side:
code
function sum(...numbers) { return numbers.reduce((total, n) => total + n, 0);}sum(1, 2, 3); // 6sum(10, 20, 30, 40); // 100
You can combine regular parameters with rest — but rest must come last:
code
function log(level, ...messages) { // level is the first argument // messages is an array with the rest messages.forEach(msg => console.log(`[${level}]`, msg));}log('INFO', 'server started', 'port 3000');// [INFO] server started// [INFO] port 3000
The difference between rest and the arguments object:
code
function withArguments() { console.log(arguments); // Arguments object — not a real array console.log([...arguments]); // array, but a workaround}function withRest(...args) { console.log(args); // real array — has .map, .filter, everything}
arguments doesn't exist in arrow functions, isn't a real array, and doesn't work with destructuring. Rest parameters solves all of that.
When do I use rest vs. spread?
Easy way to remember: rest is in the function definition (collecting what arrives), spread is at the call site (expanding what you send). They're inverse operations:
code
// rest — in the signaturefunction join(...parts) { return parts.join('-'); }// spread — at the callconst words = ['let', 'us', 'go'];join(...words); // 'let-us-go'
Gotchas: shallow copy, nested objects, and spread on strings
Shallow copy
Spread only copies the first level. If the array or object contains references — other objects, nested arrays — those references are shared, not cloned:
[PREENCHER: add personal CTA here — something like "Which spread gotcha bit you in production? The shallow copy one gets everyone at least once. Drop it in the comments — and if you spotted something wrong here, feel free to correct it :D"]