Understanding the Spread Operator(...) in JavaScript

Understanding the Spread Operator(...) in JavaScript

ยท

3 min read

In today's episode of talking about awesome JavaScript features, we are going to turn our attention to the Spread operator(...) or Spread syntax. At the end of this article, you'll find a curated list of the links to other articles I have written in the past on some other JavaScript concepts and features. In the meantime, let us focus on the Spread operator(...), what it is all about and how to use it to write better JavaScript code.

The Spread syntax is a JavaScript feature that allows objects and iterables (such as arrays and strings) to be unpacked, or expanded, which can be used to make shallow copies of data structures to increase the ease of data manipulation. It is often used in combination with destructuring.

What is the Spread operator used for?

This operator can be used to carry out many routine tasks easily. The spread operator can be used to do the following:

  • Copying an array

  • Concatenating or combining arrays

  • Using Math functions

  • Using an array as arguments

  • Adding an item to a list

  • Combining objects

Let's look at an example of each of these use cases.

Copying an array

Using the Spread operator is a convenient way to copy an array or combine arrays, and it can even add new items.

const animals = ['๐Ÿฆ','๐Ÿ˜','๐Ÿˆ','๐Ÿ•โ€๐Ÿฆบ','๐Ÿ‡']
const moreAnimals = [...animals];
console.log(moreAnimals) // Array(5) ['๐Ÿฆ','๐Ÿ˜','๐Ÿˆ','๐Ÿ•โ€๐Ÿฆบ','๐Ÿ‡']

Combining or Concatenating arrays

The spread operator can be used to quickly combine two arrays.

const myArray = [1,2,3];
const yourArray = [4,5];
const ourArray = [...myArray,...yourArray];

console.log(ourArray); // Array(5) [ 1, 2, 3, 4, 5 ]

Using Math functions

In my opinion, the best way to understand the use of the spread operator is to look at the built-in functions Math.min() and Math.max(), which both expect a list of arguments, not an array. Suppose you have an array of numbers and want to get the smallest or largest number in that array, you will have to make use of Math.min() or Math.max() but passing an array to any of these functions will return NaN which isn't the answer you'd be hoping for. Instead, you should use the (...) operator to expand the array as a list of arguments in the Math function as demonstrated in the example below:

const numbers = [28, -6, 19, 0]
console.log(Math.min(numbers)) // NaN
console.log(Math.min(...numbers)) // -6
console.log(Math.max(...numbers)) // 28

Using an array as arguments

You can use the spread operator to turn an array into a list of arguments. See the example below:

function sandwich(a, b, c) {
  console.log(a); // '๐Ÿž'
  console.log(b); // '๐Ÿฅฌ'
  console.log(c); // '๐Ÿฅ“'
}

const food = ['๐Ÿž', '๐Ÿฅฌ', '๐Ÿฅ“'];

// Old way
sandwich.apply(null, food);

// โœ… ES6 way
sandwich(...food);

Adding an item to a list

The spread operator can add an item to another array with a natural, easy-to-understand syntax:

const numbers = [1, 2, 3, 4]
const moreNumbers = [...numbers, 5, 6, 7, 8]

console.log(moreNumbers) // Array(8) [1, 2, 3, 4, 5, 6, 7, 8]

Combining objects

Watch how the spread operator combines these two objects:

const myVehicle = {
  brand: 'Ford',
  model: 'Mustang',
  color: 'red'
}

const updateMyVehicle = {
  type: 'car',
  year: 2021, 
  color: 'yellow'
}

const myUpdatedVehicle = {...myVehicle, ...updateMyVehicle}

Notice the properties that did not match were combined, but the property that did match, color, was overwritten by the last object that was passed, updateMyVehicle. The resulting color is now yellow.

Conclusion

The advent of ES6 did not only make JavaScript more efficient but also more fun with the introduction of cool features like the spread operator and others. Below is the curated list of articles I promised at the beginning of this write up.