Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Spread Syntax | Section
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Essential TypeScript Basics for JavaScript Developers - 1768407373799

bookSpread Syntax

Actually, array generation is a rather complex topic for a beginner course, and there are quite a few ways to generate arrays. But I think it's a good idea to at least explore one type of array generation, so you'll know that it exists and is quite commonly used.

Array generation is needed to automatically create a new array based on a condition or another array. For example, we can set a condition that we need an array of numbers from 1 to 100 or that we only need even numbers. There are many applications, and we will come back to this when working with loops.

Let's look at an example of how to generate a new array based on an existing one:

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

We used the following syntax:

let newArray: type[] = [...array, additionalElements]

The three dots mean that we are using the old array and adding new elements to it. The old array remains unchanged, and a new updated array is created based on it, with the new values we add.

This is one of the easiest ways to generate an array. In the future, when we will discuss loops and functions, we will talk more about various methods of array generation.

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 19

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

bookSpread Syntax

Glissez pour afficher le menu

Actually, array generation is a rather complex topic for a beginner course, and there are quite a few ways to generate arrays. But I think it's a good idea to at least explore one type of array generation, so you'll know that it exists and is quite commonly used.

Array generation is needed to automatically create a new array based on a condition or another array. For example, we can set a condition that we need an array of numbers from 1 to 100 or that we only need even numbers. There are many applications, and we will come back to this when working with loops.

Let's look at an example of how to generate a new array based on an existing one:

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

We used the following syntax:

let newArray: type[] = [...array, additionalElements]

The three dots mean that we are using the old array and adding new elements to it. The old array remains unchanged, and a new updated array is created based on it, with the new values we add.

This is one of the easiest ways to generate an array. In the future, when we will discuss loops and functions, we will talk more about various methods of array generation.

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 19
some-alt