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.
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Stel mij vragen over dit onderwerp
Vat dit hoofdstuk samen
Toon voorbeelden uit de praktijk
Awesome!
Completion rate improved to 2.7
ES6 Spread Operator
Veeg om het menu te tonen
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.
Bedankt voor je feedback!