Iterating Over Arrays with the for 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:
for (let i = 0; i < array.length; i += 1) {
// Code to be executed for each array element
}
In this syntax:
let i = 0initializes a loop counter variableito zero;i < array.lengthdefines the condition for the loop to continue. It will run as long asiis less than the length of the array;i += 1increments 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:
12345const students = ["Brandon", "Mario", "Saul"]; for (let i = 0; i < students.length; i += 1) { console.log(students[i]); }
- 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 variableiand sets it to0.iis used as a loop counter;i < students.length;: This part is the condition for the loop to continue. The loop will continue as long asiis less than the length of thestudentsarray;i += 1: This part is the update statement, which increments the value ofiby1after each iteration.
- Line 4: Inside the
forloop, this line uses theconsole.log()function to log the value at the i-th index of thestudentsarray. In the first iteration,iis0, so it logs the name at index0, 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?
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain how the loop works step by step?
What happens if the array is empty?
Can I use a different variable name instead of 'i' in the loop?
Awesome!
Completion rate improved to 2.27
Iterating Over Arrays with the for Loop
Swipe to show menu
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:
for (let i = 0; i < array.length; i += 1) {
// Code to be executed for each array element
}
In this syntax:
let i = 0initializes a loop counter variableito zero;i < array.lengthdefines the condition for the loop to continue. It will run as long asiis less than the length of the array;i += 1increments 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:
12345const students = ["Brandon", "Mario", "Saul"]; for (let i = 0; i < students.length; i += 1) { console.log(students[i]); }
- 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 variableiand sets it to0.iis used as a loop counter;i < students.length;: This part is the condition for the loop to continue. The loop will continue as long asiis less than the length of thestudentsarray;i += 1: This part is the update statement, which increments the value ofiby1after each iteration.
- Line 4: Inside the
forloop, this line uses theconsole.log()function to log the value at the i-th index of thestudentsarray. In the first iteration,iis0, so it logs the name at index0, 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?
Thanks for your feedback!