Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre ES6 Spread Operator | Getting Started: ES6
Introduction to React

bookES6 Spread Operator

The spread operator () can be easily used to assign an array's values to another array.

123
let arr = [5, 6, 7, 8, 9]; let numbers = [1, 2, 3, 4, ...arr]; console.log (numbers);
copy

Changing the position of the...arr term will change the position of data in the output:

123
let arr = [5, 6, 7, 8, 9]; let numbers = [1, 2, ...arr, 3, 4]; console.log (numbers);
copy

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:

12345
let values = [1, 7, 9]; function sum(a, b, c) { return a + b + c; } console.log (sum(...values));
copy

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.

question-icon

Fill in the gaps.

const even = [2, 4, 6, 8];
const odd = [1, 3, 5, 9];
// Write code below this line
= [,]
// White code above this line
console.log (numbers.sort ());
[1, 2, 3, 4, 5, 6, 8, 9]

Click or drag`n`drop items and fill in the blanks

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 7

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Suggested prompts:

Posez-moi des questions sur ce sujet

Résumer ce chapitre

Afficher des exemples du monde réel

Awesome!

Completion rate improved to 2.7

bookES6 Spread Operator

Glissez pour afficher le menu

The spread operator () can be easily used to assign an array's values to another array.

123
let arr = [5, 6, 7, 8, 9]; let numbers = [1, 2, 3, 4, ...arr]; console.log (numbers);
copy

Changing the position of the...arr term will change the position of data in the output:

123
let arr = [5, 6, 7, 8, 9]; let numbers = [1, 2, ...arr, 3, 4]; console.log (numbers);
copy

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:

12345
let values = [1, 7, 9]; function sum(a, b, c) { return a + b + c; } console.log (sum(...values));
copy

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.

question-icon

Fill in the gaps.

const even = [2, 4, 6, 8];
const odd = [1, 3, 5, 9];
// Write code below this line
= [,]
// White code above this line
console.log (numbers.sort ());
[1, 2, 3, 4, 5, 6, 8, 9]

Click or drag`n`drop items and fill in the blanks

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 7
some-alt