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.
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Mi faccia domande su questo argomento
Riassuma questo capitolo
Mostri esempi dal mondo reale
Awesome!
Completion rate improved to 2.7
ES6 Spread Operator
Scorri per mostrare il menu
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.
Grazie per i tuoi commenti!