Course Content
Introduction to React
Introduction to React
ES6 Spread Operator
The spread operator (…
) can be easily used to assign an array's values to another array.
let arr = [5, 6, 7, 8, 9]; let numbers = [1, 2, 3, 4, ...arr]; console.log (numbers);
Changing the position of the...arr
term will change the position of data in the output:
let arr = [5, 6, 7, 8, 9]; let numbers = [1, 2, ...arr, 3, 4]; console.log (numbers);
The spread operator …
unpacks the array arr
elements and places them at the location. This can be used in many flexible ways, like passing arguments to a function:
let values = [1, 7, 9]; function sum(a, b, c) { return a + b + c; } console.log (sum(...values));
Task
Complete the following code by creating a new array called numbers
and append the elements of both even
and odd
arrays (in this order) to the numbers
array. Do it in a single line of code.
Thanks for your feedback!