The spread operator is a favorite of JavaScript developers. It's a powerful piece of syntax that has numerous applications.
This is one of the most common uses of the spread operator. Taking the contents of an array and "spreading it out" to fill another array.
let arr = [1,2,3,4] let copy = [...arr] // copy is [ 1, 2, 3, 4 ]
Looked at in a different way, the spread operator is selecting each individual element inside the arr
array and placing each of those elements in a new array structure.
Note that this is different than putting an array inside another array.
let arr = [1,2,3,4] let copy = [arr] // copy is [ [1, 2, 3, 4] ]
That option gives you a multidimensional array.
Building on the previous example, it turns out that you're able to take multiple arrays and spread them out in a new array. One after another.
let arr1 = [1,2,3,4] let arr2 = [5,6,7,8] let concat = [...arr1, ...arr2] // concat is [ 1, 2, 3, 4, 5, 6, 7, 8 ]
If we break it down as we did in the previous example, the spread operator is extracting each element in the initial arrays and placing it in the new array.
This is where the spread operator starts showing off its versatility. In this example, we're passing three arguments into a function. The spread operator is used in front of an array with three elements inside of it.
function dev(x, y, z) { }var args = [0, 1, 2]
dev(…args) // call function
A good way to make sense of this is to look at our previous examples. What would happen if we used the spread operator on an array and never placed it inside a new array?
Each element in the array would stand on its own. This is that intermediate transformation. Each element stands on its own and hasn’t been placed in a new data structure. Therefore, all three elements can be passed in as arguments to the function individually.
Not only can the spread operator be used for arrays, it can be used for objects as well. Just like copying the array before, we can copy an object.
let obj = {a: 1, b: 2, c: 3}
let copy = {…obj}
// copy is {a: 1, b: 2, c: 3}
In this example, the spread operator extracts each key-value pair from obj
and places them in a new object, copy
.
And just like the array example, it’s worth noting that this is different than putting an object inside another object.
let obj = {a: 1, b: 2, c: 3}
let copy = {obj}
// copy is { {a: 1, b: 2, c: 3} }
We can also merge two objects together using the spread operator.
let obj1 = {a: 1, b: 2, c: 3}
let obj2 = {d: 4, e: 5, f: 6}let merge = {…obj1, …obj2}
// merge is {a: 1, b: 2, c: 3, d: 4, e: 5, f: 6}
Again, we’re extracting all the key-value pairs from the initial objects and placing them in a new object data structure.
Despite the fact that the spread operator works on both arrays and objects, you can’t mix and match these data types.
let obj = {a:1, b:2, c:3}
let copy = […obj] // this won’t work!
This makes sense if we think about it because when working on an array the spread operator is handling elements, and for an object, it’s key-value pairs.
That’s a whole lot of uses for the spread operator, and those aren’t even all of them. If you’re looking for a full set, take a look at the mozilla docs.
Thanks for reading ❤
If you liked this post, please do share/like it with all of your programming buddies!
Follow us on Facebook | Twitter
☞ The Complete JavaScript Course 2019: Build Real Projects!
☞ Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
☞ JavaScript Bootcamp - Build Real World Applications
☞ JavaScript Programming Tutorial - Full JavaScript Course for Beginners
☞ New ES2019 Features Every JavaScript Developer Should Know
☞ Best JavaScript Frameworks, Libraries and Tools to Use in 2019
☞ What JavaScript Framework You Should Learn to Get a Job in 2019?
☞ Best JavaScript Frameworks, Libraries and Tools to Use in 2019
#javascript #arrays #web-development