start learning
Image 1

For loop

The "for loop" in JavaScript is a looping construct that allows you to execute a block of code repeatedly for a specific number of iterations. It's especially useful when you know the exact number of times you want to loop. The "for loop" consists of three parts: initialization, condition, and increment (or decrement).

The basic syntax of a "for loop" in JavaScript is as follows:

for (initialization; condition; increment/decrement) {
    // Code to execute in each iteration

Here's how the "for loop" works:

  1. The initialization sets up the loop by initializing a variable. It typically defines a starting point for the loop.
  2. The condition is evaluated before each iteration. If the condition is true, the loop's block of code is executed; otherwise, the loop terminates.
  3. After each iteration, the increment (or decrement) is applied to the loop control variable, updating its value for the next iteration.
  4. The loop continues as long as the condition remains true.

for (var i = 0; i < 5; i++) {
    console.log("Iteration " + i);

In this example, the loop initializes i to 0, checks if i is less than 5, executes the code block, increments i by 1, and then repeats the process until i is no longer less than 5.

The "for loop" is useful when you have a clear idea of the number of times you want to loop and need precise control over initialization, condition checking, and incrementing.