What is the spread operator in javascript?

The spread operator in JavaScript is useful for adding elements to an array, combining arrays and more!

Here’s an example:

let arr1 = [1,2,3];
let arr2 = [4,5];
let arr3 = […arr1,…arr2];
console.log(arr3);
// Output: [ 1, 2, 3, 4, …


This content originally appeared on DEV Community and was authored by Code_Jedi

The spread operator in JavaScript is useful for adding elements to an array, combining arrays and more!

Here's an example:

let arr1 = [1,2,3]; 
let arr2 = [4,5]; 
let arr3 = [...arr1,...arr2]; 
console.log(arr3);
// Output: [ 1, 2, 3, 4, 5 ] 

As you can see the spread operator(represented as "...") was able to combine "arr1" and "arr2" into "arr3".

The spread operator can also expand arrays:

let arr = ["a","b"]; 
let arr2 = [...arr,"c","d"]; 
console.log(arr2); 
// Output: ["a", "b", "c", "d"]

As you can see, the spread operator here was able to add elements "c" and "d" to "arr", thus creating a bigger array.

But that's not all!

The spread operator can also merge objects:

const Person = { 
    name: "Bob", 
    age: 45,
}; 
const ExtraInfo = { 
    country: "Belgium", 
    city: "Brussels", 
}; 
const user = {... Person, ... ExtraInfo};
// Output: {  name: "Bob",  age: 45, country: "Belgium", city: "Brussels" }

There it is, the javascript spread operator.

Get the hottest programming stuff of the week in your inbox every Friday via my newsletter!

...

Byeeeee?


This content originally appeared on DEV Community and was authored by Code_Jedi


Print Share Comment Cite Upload Translate Updates
APA

Code_Jedi | Sciencx (2021-07-15T19:14:09+00:00) What is the spread operator in javascript?. Retrieved from https://www.scien.cx/2021/07/15/what-is-the-spread-operator-in-javascript/

MLA
" » What is the spread operator in javascript?." Code_Jedi | Sciencx - Thursday July 15, 2021, https://www.scien.cx/2021/07/15/what-is-the-spread-operator-in-javascript/
HARVARD
Code_Jedi | Sciencx Thursday July 15, 2021 » What is the spread operator in javascript?., viewed ,<https://www.scien.cx/2021/07/15/what-is-the-spread-operator-in-javascript/>
VANCOUVER
Code_Jedi | Sciencx - » What is the spread operator in javascript?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/15/what-is-the-spread-operator-in-javascript/
CHICAGO
" » What is the spread operator in javascript?." Code_Jedi | Sciencx - Accessed . https://www.scien.cx/2021/07/15/what-is-the-spread-operator-in-javascript/
IEEE
" » What is the spread operator in javascript?." Code_Jedi | Sciencx [Online]. Available: https://www.scien.cx/2021/07/15/what-is-the-spread-operator-in-javascript/. [Accessed: ]
rf:citation
» What is the spread operator in javascript? | Code_Jedi | Sciencx | https://www.scien.cx/2021/07/15/what-is-the-spread-operator-in-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.