ReviseAlgo Logo

ES6+ Modern JavaScript

Spread & Rest Operators (Deep Dive)

Master ES6 spread and rest operators in JavaScript. Understand shallow copying, array/object composition, and variable argument handling.

Last Updated: July 15, 2026 10 min read

1. Introduction

The spread and rest operators both use the three-dots syntax (...). However, they perform opposite operations: the spread operator expands collections into individual elements, while the rest operator condenses multiple elements into a single collection.

2. Why It Matters

The three-dots syntax is widely used in modern JavaScript for shallow copying objects, merging arrays, and passing variable numbers of arguments to functions. Knowing when to use spread and rest is key to writing clean code.

3. Real-World Analogy

Think of a Pill Organizer Case:

  • Spread Operator (Emptying a box): Opening a pre-packed pill box and scattering the individual pills across your workspace (unpacking a collection).
  • Rest Operator (Packing a box): Gathering all the loose pills lying around on your workbench and placing them together into a single travel container (collecting individual items into a single collection).

4. The Spread Operator

The spread operator unpacks elements of an array or properties of an object.

1. Array Spread:

Create copies, concatenate arrays, or pass elements as arguments to functions.

2. Object Spread:

Shallow copy properties of objects or merge multiple objects. Properties defined later overwrite properties with the same name defined earlier.

5. The Rest Operator

The rest operator collects multiple elements into a single array or object.

1. Rest Parameters in Functions:

Allows a function to accept a variable number of arguments as a single array. The rest parameter must be the last parameter in the function signature.

2. Rest in Destructuring:

Collects remaining properties of an object or elements of an array during destructuring.

6. Practical Example

This script demonstrates using the rest operator to split an API response object into a key identifier and a remaining configuration object:

7. Common Mistakes

  • Placing the rest parameter before other parameters: The rest parameter must be the last parameter in the function signature. Placing it before other parameters (e.g. (...args, final) => {}) throws a SyntaxError.
  • Expecting deep copies: The spread operator only creates a shallow copy. If the source object contains nested objects or arrays, the copied object will still share references to those nested objects. Modifying nested properties in the copied object will mutate the original object.

8. Quick Quiz

Q1: What happens to nested object references when you copy an object using the spread operator?

A) A deep copy is created; nested objects are cloned completely

B) Only a shallow copy is created; nested objects still share memory references

Answer: B — The spread operator only copies properties shallowly. Nested objects or arrays still share references with the original object.

9. Scenario-Based Challenge

The Immature Reducer State Update:

In a state manager (like Redux), you receive the current state: { user: { profile: { login: "alice" } }, status: "idle" }. You want to update the status to "loading" without mutating the original state object. Write the update using the spread operator.

10. Debugging Exercise

Explain why this parameter list throws a SyntaxError, and how to fix it:

// Objective: handle primary command plus optional inputs
function runCommand(...options, commandName) { // SyntaxError! Why?
  console.log(commandName, options);
}
View Solution

Diagnosis: The rest parameter ...options must be the last parameter in the function signature. Placing another parameter after the rest parameter throws a SyntaxError: Rest parameter must be last formal parameter.

Fix: Place the rest parameter at the end of the parameter list:

function runCommand(commandName, ...options) { // Correct order
  console.log(commandName, options);
}

11. Interview Questions

🟢 Q1: Describe the difference between spread and rest operators and how their purposes differ.

Answer:
Spread Operator: Unpacks elements of an array or properties of an object into individual elements. It is used to copy arrays/objects, concatenate arrays, or pass array elements as arguments to functions.
Rest Operator: Gathers multiple elements or arguments together into a single array or object. It is used in function signatures to accept a variable number of arguments, or in destructuring to capture remaining properties.

12. Production Considerations

  • Performance of Copying: Using the spread operator to clone massive objects or arrays inside high-frequency loops can slow down performance and increase memory usage. For large datasets, modify elements in place or use structured data structures instead.