In JavaScript, the for loop is used to execute a block of code repeatedly for a specified number of times.
Here is the syntax of the for loop:
The initialization statement is executed only once at the beginning of the loop. It initializes the loop variable(s).
The condition statement is evaluated before each iteration. If it is true, the loop continues. If it is false, the loop terminates.
The increment/decrement statement is executed after each iteration. It updates the loop variable(s).
Here is an example of a for loop that iterates from 0 to 4 and prints the value of the loop variable i to the console:
In the above example, let i = 0 initializes the loop variable i to 0, i < 5 is the condition that checks if i is less than 5, and i++ increments the value of i by 1 after each iteration.
The for loop can also be used to iterate over an array. Here is an example:
In the above example, arr.length is the condition that checks if i is less than the length of the array, and arr[i] accesses the element at the current index i of the array.
That's it for JavaScript for loop. Thanks for reading and learning ❤️
#LetsCodeIt