Spread Operator

The main objective of the spread operator is to spread the elements of an array or object. This is best explained with examples.

Apply

A common use case is to spread an array into the function arguments. Previously you would need to use Function.prototype.apply:

  1. function foo(x, y, z) { }
  2. var args = [0, 1, 2];
  3. foo.apply(null, args);

Now you can do this simply by prefixing the arguments with ... as shown below:

  1. function foo(x, y, z) { }
  2. var args = [0, 1, 2];
  3. foo(...args);

Here we are spreading the args array into positional arguments.

Destructuring

We’ve already seen one usage of this in destructuring:

  1. var [x, y, ...remaining] = [1, 2, 3, 4];
  2. console.log(x, y, remaining); // 1,2,[3,4]

The motivation here is to simply make it easy for you to capture the remaining elements of an array when destructuring.

Array Assignment

The spread operator allows you to easily place an expanded version of an array into another array. This is demonstrated in the example below:

  1. var list = [1, 2];
  2. list = [...list, 3, 4];
  3. console.log(list); // [1,2,3,4]

You can put the expanded array in at any position, and get the effect you’d expect:

  1. var list = [1, 2];
  2. list = [0, ...list, 4];
  3. console.log(list); // [0,1,2,4]

Object spread

You can also spread an object into another object. A common use case is to simply add a property to an object without mutating the original:

  1. const point2D = {x: 1, y: 2};
  2. /** Create a new object by using all the point2D props along with z */
  3. const point3D = {...point2D, z: 3};

For objects, the order of where you put the spread matters. This works something like Object.assign, and does what you’d expect: what comes first is ‘overridden’ by what comes later:

  1. const point2D = {x: 1, y: 2};
  2. const anotherPoint3D = {x: 5, z: 4, ...point2D};
  3. console.log(anotherPoint3D); // {x: 1, y: 2, z: 4}
  4. const yetAnotherPoint3D = {...point2D, x: 5, z: 4}
  5. console.log(yetAnotherPoint3D); // {x: 5, y: 2, z: 4}

Another common use case is a simple shallow extend:

  1. const foo = {a: 1, b: 2, c: 0};
  2. const bar = {c: 1, d: 2};
  3. /** Merge foo and bar */
  4. const fooBar = {...foo, ...bar};
  5. // fooBar is now {a: 1, b: 2, c: 1, d: 2}

Summary

apply is something that you often use in JavaScript, so it’s good to have a better syntax where you don’t have that ugly null for the this argument. Also having a dedicated syntax for moving arrays out of (destructuring) or into (assignment) other arrays provides a neat syntax for when you are doing array processing on partial arrays.