Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Using Loops with Arrays | Discovering Loops
Introduction to JavaScript
course content

Contenu du cours

Introduction to JavaScript

Introduction to JavaScript

1. Getting Started
3. Conditional Statements
4. Mastering Functions
5. Exploring Arrays
6. Discovering Loops

book
Using Loops with Arrays

Loops enable us to efficiently go through all or specific elements of an array.

For example, we can use a for loop to iterate through all elements of an array and output them:

12345
let numbers = [1, 1, 2, 3, 5, 8, 13, 21]; for(let i = 0; i < numbers.length; i++) { console.log("Element " + (i + 1) + " of the array is: " + numbers[i]); }
copy

This is especially useful when we need to perform an operation on multiple elements in an array:

1234567
let numbers = [1, 1, 2, 3, 5, 8, 13, 21]; console.log("Before:", numbers); for(let i = 0; i < numbers.length; i++) { numbers[i] *= 2; } console.log("After:", numbers);
copy

We can use a while or a do-while loop for this purpose as well, however, that is not the convention.

123456789
let numbers = [1, 1, 2, 3, 5, 8, 13, 21]; console.log("Before:", numbers); let i = 0; while(i < numbers.length) { numbers[i] *= 2; i += 1; } console.log("After:", numbers);
copy

Even though the same results can be achieved using while or do-while loops, it is recommended to use a for loop when iterating through arrays because it is the conventional and more readable approach.

1. What does the following code output?

2. Does the following code modify the original array?

question mark

What does the following code output?

Select the correct answer

question mark

Does the following code modify the original array?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 6. Chapitre 7

Demandez à l'IA

expand
ChatGPT

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

course content

Contenu du cours

Introduction to JavaScript

Introduction to JavaScript

1. Getting Started
3. Conditional Statements
4. Mastering Functions
5. Exploring Arrays
6. Discovering Loops

book
Using Loops with Arrays

Loops enable us to efficiently go through all or specific elements of an array.

For example, we can use a for loop to iterate through all elements of an array and output them:

12345
let numbers = [1, 1, 2, 3, 5, 8, 13, 21]; for(let i = 0; i < numbers.length; i++) { console.log("Element " + (i + 1) + " of the array is: " + numbers[i]); }
copy

This is especially useful when we need to perform an operation on multiple elements in an array:

1234567
let numbers = [1, 1, 2, 3, 5, 8, 13, 21]; console.log("Before:", numbers); for(let i = 0; i < numbers.length; i++) { numbers[i] *= 2; } console.log("After:", numbers);
copy

We can use a while or a do-while loop for this purpose as well, however, that is not the convention.

123456789
let numbers = [1, 1, 2, 3, 5, 8, 13, 21]; console.log("Before:", numbers); let i = 0; while(i < numbers.length) { numbers[i] *= 2; i += 1; } console.log("After:", numbers);
copy

Even though the same results can be achieved using while or do-while loops, it is recommended to use a for loop when iterating through arrays because it is the conventional and more readable approach.

1. What does the following code output?

2. Does the following code modify the original array?

question mark

What does the following code output?

Select the correct answer

question mark

Does the following code modify the original array?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 6. Chapitre 7
Nous sommes désolés de vous informer que quelque chose s'est mal passé. Qu'est-il arrivé ?
some-alt