DEV Community

Cover image for Demystifying the Ellipsis (...): Spread Operator vs Rest Operator in JavaScript
Raju Saha
Raju Saha

Posted on

Demystifying the Ellipsis (...): Spread Operator vs Rest Operator in JavaScript

The ellipsis (...) in JavaScript might seem like a simple punctuation mark, but it unlocks two powerful operators: the Spread Operator and the Rest Operator. Both utilize the ellipsis but serve distinct purposes. Let's delve into their functionalities and explore how they can elevate your code!

1. Spread Operator (...): The Art of Scattering

Imagine you have an array of tasty ingredients and want to share them across multiple recipes. The Spread Operator allows you to unpack the elements of an iterable (like arrays, strings, or objects) and scatter them into individual elements within another expression.

Common Use Cases:

Merging Arrays:

const fruits = ['apple', 'banana'];
const vegetables = ['carrot', 'potato'];
const allIngredients = [...fruits, ...vegetables]; // allIngredients = ['apple', 'banana', 'carrot', 'potato']

Enter fullscreen mode Exit fullscreen mode

Copying Arrays (to avoid accidental modification):

const originalArray = [1, 2, 3];
const copyArray = [...originalArray]; // copyArray = [1, 2, 3] (independent copy)

Enter fullscreen mode Exit fullscreen mode

Spreading Function Arguments:

function sum(x, y, z) {
  return x + y + z;
}

const numbers = [1, 2, 3];
const result = sum(...numbers); // result = 6 (spreads numbers array as individual arguments)

Enter fullscreen mode Exit fullscreen mode

2. Rest Operator (...): The Art of Gathering

Think of the Rest Operator as the opposite of the Spread Operator. It gathers the remaining elements from an iterable into a single array. Imagine collecting leftover ingredients after preparing a dish.

Common Use Cases:
Creating Functions with Variable Arguments:

function logNumbers(...numbers) {
  console.log(numbers); // logs all passed arguments as an array
}

logNumbers(1, 2, 3, 4); // logs [1, 2, 3, 4]

Enter fullscreen mode Exit fullscreen mode

Destructuring Arrays:

const [first, ...rest] = [1, 2, 3, 4];
console.log(first); // 1
console.log(rest); // [2, 3, 4] (rest captures remaining elements)

Enter fullscreen mode Exit fullscreen mode

Choosing the Operator:

The choice between Spread and Rest depends on your intention. Use Spread to unpack iterables and distribute elements, while Rest gathers the remaining elements into a single array.

Remember, the Spread Operator can also be used to spread object properties when creating new objects or merging existing ones.

Embrace the Ellipsis!

By mastering the Spread and Rest Operators, you'll enhance your ability to manipulate data structures, write concise code, and improve code readability. So, the next time you encounter the ellipsis (...), wield its power with confidence!

Top comments (1)

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

...unpack the elements of an iterable (like arrays, strings, or objects)...

Not quite right, an object is usually not an iterable. From MDN:

In order to be iterable, an object must implement the @@iterator method, meaning that the object (or one of the objects up its prototype chain) must have a property with a @@iterator key which is available via constant Symbol.iterator

If the object IS an iterable, then it is capable of being spread in an array context as well as in an object context. If it isn't, an error 'obj is not iterable' will result in the array context.

So it would be better to say '...unpack the elements of an object or iterable (like...'