← Back to JavaScript
17

Spread and rest: copy, merge and group data without dangerous mutations

Master the spread (`...`) and rest operators so you can work with arrays and objects immutably, build flexible functions and avoid bugs caused by shared references.

📘 Theory

Practical immutability, not empty theory

Spread and rest help you create new structures instead of modifying the old ones.

In reactive interfaces, mutating original state often creates behavior that is difficult to trace. Controlled copies make changes more predictable.

A useful rule is simple: if a value is shared by several parts of the system, avoid mutating it directly.

1

Spread in arrays

Copy or merge lists without touching the source.

  • `const copy = [...original]`
  • `const all = [...a, ...b]`
2

Spread in objects

Create new objects with explicit overrides.

  • `const copy = { ...obj }`
  • `const updated = { ...obj, active: true }`
3

Rest in functions

Group variable arguments into one real array.

  • `function sum(...nums) {}`
  • Useful in flexible helpers

Spread in arrays and objects

The most common use is to update state without mutating the original reference.

1

In arrays, spread helps you add, merge or reorder values while creating a new reference. In objects, spread lets you copy the existing shape and overwrite a few fields clearly.

2

The important caveat is that spread only creates a shallow copy. Nested objects are still shared unless you handle them separately.

Rest parameters and shared references

Rest makes function signatures flexible, and reference checks explain many hidden bugs.

1

Rest groups variable arguments into a real array, which makes iteration and validation straightforward. It is much cleaner than relying on the old `arguments` object.

2

At the same time, you need to understand references: assigning `const b = a` does not clone an array or object. It copies the reference, so mutating one affects the other.

🧪 Learn by doing

Example Guided example: a cart without mutation Add products and apply a discount while always creating new structures.
Example Interactive demo: combine and clean tags Write comma-separated tags, add more and see how immutable copies and `Set` keep the result clean.

🏁 Challenges

Challenge Challenge 1: update a user without mutating the original Create `updatedUser` with spread, changing only `active` and `role`.

What is this?

I'm Cristian Eslava and I sometimes build websites so both you and I can learn and experiment. culTest

I made this in February 2026 to make learning easier for my students. The idea is to learn web development by practicing and to keep expanding the project with new topics, tests and challenges.

It draws inspiration from MDN, W3Schools, CodePen, Manz and many other web development references. I wanted to combine useful theory, runnable examples, challenges and the testing system I had already built for culTest. culTest

If you liked it, if you didn't, or if you want to get in touch, write to me at cristianeslava@gmail.com