Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
for Loop | Mastering Arrays
JavaScript Data Structures
course content

Contenido del Curso

JavaScript Data Structures

JavaScript Data Structures

1. Introduction and Prerequisites
2. Objects Fundamentals
3. Advanced Object Manipulation
4. Mastering Arrays
5. Advanced Array Operations

bookfor Loop

Array iteration is a crucial concept when working with arrays. It allows us to process each element in an array, perform operations, and make decisions based on their values. In this chapter, we will explore for loop for iterating over arrays.

Syntax

The for loop is a fundamental tool for iterating over an array element by element. It allows us to access each element in the array by element index. The syntax for a for loop is as follows:

In this syntax:

  • let i = 0 initializes a loop counter variable i to zero;
  • i < array.length defines the condition for the loop to continue. It will run as long as i is less than the length of the array;
  • i += 1 increments the loop counter after each iteration.

Example

Here's an example of using the for loop to access and display the elements of the students array:

12345
const students = ["Brandon", "Mario", "Saul"]; for (let i = 0; i < students.length; i += 1) { console.log(students[i]); }
copy
  • Line 1: This line declares an array called students and initializes it with three strings, which represent the students' names. The array contains "Brandon", "Mario", and "Saul";
  • Line 3: This line starts a for loop. It has three parts separated by semicolons:
    • let i = 0;: This part initializes a variable i and sets it to 0. i is used as a loop counter;
    • i < students.length;: This part is the condition for the loop to continue. The loop will continue as long as i is less than the length of the students array;
    • i += 1: This part is the update statement, which increments the value of i by 1 after each iteration.
  • Line 4: Inside the for loop, this line uses the console.log() function to log the value at the i-th index of the students array. In the first iteration, i is 0, so it logs the name at index 0, which is "Brandon". In the second iteration, it logs "Mario", and in the third iteration, it logs "Saul".
1. What is the purpose of a `for` loop when working with arrays?
2. What should be the condition for the loop to iterate through the entire `computers` array?
3. What should be the condition for the loop to iterate through the `computers` array to show only the first two elements?
What is the purpose of a `for` loop when working with arrays?

What is the purpose of a for loop when working with arrays?

Selecciona la respuesta correcta

What should be the condition for the loop to iterate through the entire `computers` array?

What should be the condition for the loop to iterate through the entire computers array?

Selecciona la respuesta correcta

What should be the condition for the loop to iterate through the `computers` array to show only the first two elements?

What should be the condition for the loop to iterate through the computers array to show only the first two elements?

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 4. Capítulo 5
We're sorry to hear that something went wrong. What happened?
some-alt