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

Kursusindhold

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

book
ES6 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

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 7

Spørg AI

expand
ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

course content

Kursusindhold

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

book
ES6 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

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 7
Vi beklager, at noget gik galt. Hvad skete der?
some-alt