- In JavaScript, a for loop is a control structure that allows you to execute a block of code repeatedly for a specified number of iterations. It's a fundamental loop construct used to automate repetitive tasks and iterate through a sequence of values, typically an array or a numerical range.
for (initialization; condition; iteration) {
// Code to be executed in each iteration
// This code block can contain one or more statements
}
// Code outside the loop
- Here's how the components of the for loop work:
- Initialization: This part is used to initialize a loop control variable (usually called an index or a counter). It's executed before the loop starts and is typically used to set the initial value of the control variable.
- Condition: The condition is a boolean expression that determines whether the loop should continue or stop. It's evaluated before each iteration, and if the condition is true, the loop continues; if it's false, the loop terminates.
- Iteration: The iteration part is executed after each iteration of the loop. It's typically used to update the loop control variable (e.g., increment or decrement the index).
- Code Block: The code block contains the statements that are executed in each iteration of the loop. This is where you place the code that you want to repeat.
- Code outside the loop: This code is executed after the loop finishes executing. It's outside the scope of the loop.
for (let i = 0; i < 5; i++) {
console.log("Iteration:", i);
}
-
In this example, the loop will execute five times because the condition i < 5 starts as true and becomes false after the fifth iteration (when i becomes 5). The output will be: Iteration: 0 Iteration: 1 Iteration: 2 Iteration: 3 Iteration: 4
-
For loops are incredibly versatile and are commonly used to iterate over arrays, generate sequences, perform calculations over time, and much more. They're a powerful tool for automating repetitive tasks in your JavaScript programs.
- Connect your js file to your HTML file
- Open your code in the browser and open your console
- In the
js
file, complete the list of exercises.