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

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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.