Spread and Rest Operators


The spread and rest operators use the same syntax (...) but they are used for different purposes in JavaScript.

The spread operator is used to expand values from arrays or objects.  

The rest operator is used to collect multiple values into a single variable.

These operators make JavaScript code cleaner, more flexible, and easier to work with.

Difference Between Spread and Rest Operators

Spread Operator (...)

  • Expands values 
  • Used in arrays, objects, and function calls  
  • Helps copy and merge data  

Rest Operator (...)

  • Collects values 
  • Used in function parameters
  • Helps handle unlimited arguments  

Both operators improve code readability and flexibility.

Here, the spread operator expands the numbers array into a new array.

This is a clean way to copy or extend arrays.

The spread operator copies all properties from one object into another.

This is commonly used when updating objects in modern JavaScript.

These examples show how spread and rest operators simplify working with data and function arguments.

By mastering spread and rest operators, students gain the ability to write flexible, modern, and professional JavaScript code used in real-world applications.