JavaScript for
Loop (Live Playground)
In this tutorial, we will explore the for
loop, and learn how to use it in our code to help perform repetitive tasks.
for
Loop
A for
loop is used to execute a block of code a specific number of times. It consists of an initialization, a condition, and an update expression.
Example:
for (let i = 0; i < 5; i++) {
console.log(`Iteration ${i + 1}`);
}
In the example above, the loop will run 5 times, and the output will display the iteration number from 1 to 5.
Live Playground, Try it Yourself
Conclusion
Understanding and using the for
loop is essential for performing repetitive tasks in your JavaScript programs.