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:
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