# The spread operator (the three dots of JavaScript) and its uses

0

Last updated: February 9, 2022.

You may have noticed the appearance of three little dots (`...`) in some JavaScript code and what it means. This is the spread operator and is used for ‘spreading’ the individual elements of an iterable:

```const myArray = [4, 6, 3, 5, 3, 5];

console.log(myArray);
// Prints the array object:
// [4,6,3,5,3,5]

console.log(...myArray);
// Prints the individual elements of the array:
// 4 6 3 5 3 5```

This may not seem like such a big deal, but there are a whole load of use cases where these three little dots prove extremely useful.

## Uses of the spread operator

### #1: Copy a simple array

By spreading the individual elements of an array into a new array, the spread operator provides a fast and clean way to make an array copy:

```const myArray = [4, 6, 3, 5, 3, 5];

const arrayCopy = [...myArray];

console.log(arrayCopy)
// [4, 6, 3, 5, 3, 5]```

You might intuitively think that there is a simpler way to do this: set the value of `arrayCopy` to `myArray`. But in JavaScript, this only creates a reference to the original array. Thus, if the copy is edited, the original is also edited:

```const myArray = [4, 6, 3, 5, 3, 5];

const arrayCopy = myArray;

arrayCopy[0] = "πΆ";

console.log(arrayCopy); // βοΈ
// ["πΆ",6,3,5,3,5]
console.log(myArray); // π±
// ["πΆ",6,3,5,3,5]```

The behavior in the above code is almost always unwanted. The spread operator helps us to make a better array copy:

```const myArray = [4, 6, 3, 5, 3, 5];

const arrayCopy = [...myArray];

arrayCopy[0] = "πΆ";

console.log(arrayCopy); // βοΈ
// ["πΆ",6,3,5,3,5]
console.log(myArray); // βοΈ
// [4,6,3,5,3,5]```

But if your array contains nested properties, the referencing problem emerges for the nested properties even when using the spread operator:

```const myArray = [4, 6, 3, 5, [3, 5]];

const arrayCopy = [...myArray];

arrayCopy[4][1] = "πΆ";

console.log(arrayCopy); // βοΈ
// ["πΆ",6,3,5,3,5]
console.log(myArray); // π±
// ["πΆ",6,3,5,3,5]```

To make a copy of an array that contains nesting, we recommend using the `.cloneDeep` method from the third-party Lodash library.

The following code works if Lodash is already loaded from a CDN:

```// Lodash already loaded in earlier script tag

const myArray = [4, 6, 3, 5, [3, 5]];

const arrayCopy = _.cloneDeep(myArray);

arrayCopy[4][1] = "πΆ";

console.log(arrayCopy); // βοΈ
// [4, 6, 3, 5, [3, "πΆ"]]
console.log(myArray); // βοΈ
// [4, 6, 3, 5, [3, 5]]```

### #2: Merge arrays

Existing arrays can be merged into a new one by spreading their individual elements into a new array:

```const dogArray = ["πΆ", "πΆ", "πΆ"];
const catArray = ["π±", "π±", "π±"];

const animalArray = [...dogArray, ...catArray];

console.log(animalArray);
// ["πΆ","πΆ","πΆ","π±","π±","π±"]```

The spread operator can also be used in combination with normally specified array elements:

```const catArray = ["π±", "π±", "π±"];

const catArray2 = [...catArray, "Kitty", "Cat", "Meow"];

console.log(catArray2);
// ["π±","π±","π±","Kitty","Cat","Meow"]```

For completeness, here's what would happen if we didn't use the spread operator in the first example:

```const dogArray = ["πΆ", "πΆ", "πΆ"];
const catArray = ["π±", "π±", "π±"];

const animalArray = [dogArray, catArray];

console.log(animalArray);
// [["πΆ","πΆ","πΆ"],["π±","π±","π±"]]```

We end up with an array containing two array objects: `dogArray` and `catArray`.

### #3: With Math.min() and Math.max()

The `.min()` and `.max()` methods available on the `Math` object accept a list of numbers and returns the lowest or largest number from the list. Try passing in an array object and `NaN` will be returned because although it contains a list of numbers, it is itself an object.

The spread operator provides a simple way to get around this problem by spreading array values into `Math.min()` and `Math.max()`:

```const myNumberArray = [5,2,7,4,4,2,7,8,2,3,5];

const minValue = Math.min(...myNumberArray);
// Is the same as: Math.min(5,2,7,4,4,2,7,8,2,3,5)

const maxValue = Math.max(...myNumberArray);
// Is the same as: Math.max(5,2,7,4,4,2,7,8,2,3,5)

console.log(minValue); // 2
console.log(maxValue); // 8```

And here's the wrong way to do it:

```// β Math.min or Math.max do not work if an array object is passed in:
const myNumberArray = [5,2,7,4,4,2,7,8,2,3,5];

const minValue = Math.min(myNumberArray);
const maxValue = Math.max(myNumberArray);

console.log(minValue); // NaN
console.log(maxValue); // NaN```

### #4: Create a function with an unknown number of arguments

A function accepts parameters as a comma-separated list.

By using the spread operator, we can create a function with an unspecified number of arguments passed in when called:

```function(...args) {
// function statement
}```

When is this useful? For example, when we want to join a list of string values together inside a function, but are unsure how many will be passed in each time:

```function printFullName (...args) {
const fullName = args.join(' ');
console.log(fullName);
}

printFullName("Kiefer", "William", "Frederick", "Dempsey", "George", "Rufus", "Sutherland");
// "Kiefer William Frederick Dempsey George Rufus Sutherland"

### #5: List characters of a string

Just as the spread operator can be used to list array elements, it can also list the characters of a string individually:

```const name = "Britney";

console.log(...name);
// "B" "r" "i" "t" "n" "e" "y"```

### #6: Get non-destructured elements in array and object destructuring

When destructuring an array or object, specifying `...rest `saves non-destructured elements to a new array or object called rest.

For example, here `rest` becomes all elements apart from the destructured `"Batman"`:

```const superheroes = ["π¦", "πΈοΈ", "π§²"];

const [hero1, ...rest] = superheroes;

console.log(hero1);
// "π¦"

console.log(rest);
// ["πΈοΈ", "π§²"];```

And in object destructuring, it works in the same way: a new object is produced, containing in the below example the non-destructured thumbnail and story properties:

```const newsStory = {
title: "Spiderman: Terrorist or hero?",
category: "πΈοΈ",
printTitle: function() {
}
}

const { title, ...rest } = newsStory;

console.log(title);
"Spiderman: Terrorist or hero?"

console.log(rest);
// {
//    category: "πΈοΈ",
//    printTitle: function() {
//    }
// }```

For more, you may want to check out our full tutorial on object and array destructuring.

### #7: Shallow copy an object

In the ES2018 update to JavaScript, the use of the spread operator was extended to objects.

This allows copying an object by spreading the contents of an object into a new one:

```const newsStory = {
title: "Spiderman: Terrorist or hero?",
category: "πΈοΈ",
printTitle: function() {
}
}

const dataCopy = {...newsStory};
dataCopy.category = "π°"

console.log(dataCopy); βοΈ
// {
//   title: "Spiderman: Terrorist or hero?",
//   category: "π°",
//    printTitle: function() {
//    }
// }

console.log(newsStory); βοΈ
// {
//   title: "Spiderman: Terrorist or hero?",
//   category: "πΈοΈ",
//    printTitle: function() {
//    }
// }```

This works perfectly well for copying non-nested properties, including those that contain methods.

But any nested properties in the copy are only references to the original object.

This means that if nested properties in the copy are changed, so too is the original object:

```const newsStory = {
title: "Spiderman: Terrorist or hero?",
category: "πΈοΈ",
attributes: {
author: "Mary Jane Watson",
importance: "π΄",
}
}

const dataCopy = {...newsStory};
dataCopy.attributes.importance = "π‘";

console.log(dataCopy); βοΈ

// {
//   title: "Spiderman: Terrorist or hero?",
//   category: "πΈοΈ",
//   attributes: {
//     author: "Mary Jane Watson",
//     importance: "π‘",
//   }
// }

console.log(newsStory); π¨

// {
//    title: "Spiderman: Terrorist or hero?",
//    category: "πΈοΈ",
//    attributes: {
//      author: "Mary Jane Watson",
//      importance: "π‘",
//    }
// }```

To make a deep copy of an object, including its nested properties, we recommend using the `.cloneDeep` method from the third-party Lodash library. For setup instructions, check out our full article on object cloning.

### #8: Merge objects

The properties of two or more objects can be combined by spreading their contents into a new object:

```const breakfastMenu =  {
croissant: "π₯",
bagel: "π₯―"
}

spagBowl: "π",
steamedRice: "π",
}

console.log(fullMenu); //βοΈAll properties are now in new object
// {
//  croissant: "π₯",
//  bagel: "π₯―"
//  spagBowl: "π",
//  steamedRice: "π",
// }```

Make sure that properties do not share the same key values when doing this, because otherwise properties spread earlier will be overwritten:

```// β The wrong way wish clashing property keys:

option1: "π₯",
option2: "π₯―"
}

option1: "π",
option2: "π",
}

Though incredibly useful, it is important to remember that it is not appropriate for copying arrays or objects with nesting. For this, we recommend using `.deepClone` from the Lodash library.