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

Course Content

Introduction to React

Introduction to React

1. Getting Started: ES6
2. Getting Started: JSX
3. React in Practice
4. React Components
5. Controlling Data & Content
6. Hooks
7. React Router

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

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 7
some-alt