ES6 Spread Operator
The spread operator (…
) can be easily used to assign an array's values to another array.
123let 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:
123let 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:
12345let 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.
Tak for dine kommentarer!
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Awesome!
Completion rate improved to 2.7
ES6 Spread Operator
Stryg for at vise menuen
The spread operator (…
) can be easily used to assign an array's values to another array.
123let 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:
123let 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:
12345let 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.
Tak for dine kommentarer!