JavaScript for loop start with first statement. The loop starts in position 0 ( let i = 0 )
. The loop automatically increments i for each run. The loop runs as long as i < array.length
. The for statement defines a code block that is executed as long as a condition is true.
for (statement 1; statement 2; statement 3) {
// code block to be executed
}
Code for loop start in JavaScript
Simple example code prints the array values in the console using for loop.
<!DOCTYPE html>
<html>
<head>
<script>
const cars = ["BMW", "Volvo", "Tesla", "Ford"];
for (let i = 0; i < cars.length; i++) {
console.log(cars[i])
}
</script>
</head>
</html>
Output:
![How does a for loop start in JavaScript](https://i0.wp.com/tutorial.eyehunts.com/wp-content/uploads/2022/02/How-does-a-for-loop-start-in-JavaScript.jpg?resize=172%2C147&ssl=1)
Do comment if you have any doubts or suggestions on this JS for loop topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version