# ES6 Spread Operator

ES6 introduced a new operator referred to as a spread operator, which consists of three dots (...). It allows an iterable to expand in places where more than zero arguments are expected. It gives us the privilege to obtain the parameters from an array.

Spread operator syntax is similar to the rest parameter, but it is entirely opposite of it. Let's understand the syntax of the spread operator.

Syntax
`var variablename1 = [...value];`

The three dots (...) in the above syntax are the spread operator, which targets the entire values in the particular variable.

Let us try to understand the usage of spread operator in different cases:

## Spread Operator and Array Manipulation

Here, we are going to see how we can manipulate an array by using the spread operator.

Constructing array literal

When we construct an array using the literal form, the spread operator allows us to insert another array within an initialized array.

Example
snippet
```let colors = ['Red', 'Yellow'];
let newColors = [...colors, 'Violet', 'Orange', 'Green'];
console.log(newColors);```
Output
[ 'Red', 'Yellow', 'Violet', 'Orange', 'Green' ]
Concatenating arrays

Spread operator can also be used to concatenate two or more arrays.

Example
snippet
```let colors = ['Red', 'Yellow'];
let newColors = [...colors, 'Violet', 'Orange', 'Green'];
console.log(newColors);```
Output
[ 'Red', 'Yellow', 'Violet', 'Orange', 'Green' ]
Copying an array

We can also copy the instance of an array by using the spread operator.

Example #1
snippet
```let colors = ['Red', 'Yellow'];
let newColors = [...colors];
console.log(newColors);```
Output
[ 'Red', 'Yellow' ]

If we copy the array elements without using the spread operator, then inserting a new element to the copied array will affect the original array.

But if we are copying the array by using the spread operator, then inserting an element in the copied array will not affect the original array.

Let's understand the illustration for the same.

Example #2

### Without using spread operator

snippet
```let colors = ['Red', 'Yellow'];
let newColors = colors;
newColors.push('Green');
console.log(newColors);
console.log(colors);```
Output
[ 'Red', 'Yellow', 'Green' ] [ 'Red', 'Yellow', 'Green' ]
Example #3

### Using spread operator

snippet
```let colors = ['Red', 'Yellow'];
let newColors = [...colors];
newColors.push('Green');
console.log(newColors);
console.log(colors);```
Output
[ 'Red', 'Yellow', 'Green' ] [ 'Red', 'Yellow' ]
Note
Instead of elements, the spread operator only copies the array itself to the new one. It means that the operator can do a shallow copy instead of a deep copy.

## Spread operator and Strings

Let's see how the spread operator spreads the strings. The illustration for the same is given below.

Example

Here, we have constructed an array str from individual strings.

snippet
```let str = ['A', ...'EIO', 'U'];
console.log(str);```

In the above example, we have applied the spread operator to the string 'EIO'. It spreads out each specific character of the 'EIO' string into individual characters.

We will get the following output after the execution of the above code.

Output
[ 'A', 'E', 'I', 'O', 'U' ]
Related Tutorial
Contents +
{"ft_lang_label":"__label__en","ft_lang_prob":0.7803764,"math_prob":0.6135774,"size":2350,"snap":"2022-40-2023-06","text_gpt3_token_len":501,"char_repetition_ratio":0.18797953,"word_repetition_ratio":0.11977716,"special_character_ratio":0.24212766,"punctuation_ratio":0.2098501,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9802384,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-10-03T06:00:12Z\",\"WARC-Record-ID\":\"<urn:uuid:af70d88c-e7bd-4fc0-b374-0e7cb497828a>\",\"Content-Length\":\"111661\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:a54e1791-94be-4157-9ea9-4ba1f495077b>\",\"WARC-Concurrent-To\":\"<urn:uuid:38379062-ff17-4571-9c54-1dc82f1d2fcd>\",\"WARC-IP-Address\":\"143.95.252.47\",\"WARC-Target-URI\":\"https://rookienerd.com/tutorial/es6/es6-spread-operator\",\"WARC-Payload-Digest\":\"sha1:GKPS6W7RWIMBX3DNPQWK5D4WN2CXYA24\",\"WARC-Block-Digest\":\"sha1:KIM3SMPDQCKRKEKRSKBPN72KYEXU3GMG\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-40/CC-MAIN-2022-40_segments_1664030337398.52_warc_CC-MAIN-20221003035124-20221003065124-00751.warc.gz\"}"}