JavaScript Using For Loop

The for loop is the most used loop form in JavaScript.

It consists of 3 parts..

1) Loop initialization where we initialize the counter. The initialization statement is executed before the loop starts.

2) Test expression to tell if the condition is true. If the condition is true, the code given inside the loop is executed, otherwise the control exits the loop.

3) Iteration expression with which you can increase or decrease your counter.

for ( loop start statement; test expression; repeat expression ) {
code to run if test expression is true
}

Let's examine the example below..

In the example, we started at 0 and printed the value of our "num" variable by increasing it by 1 as long as it is less than 5 .

The result will look like the following..



You May Interest

What is Difference Between Undefined and Null in Javascript ?

JavaScript Using While Loop

What is setInterval Function in Javascript ?

What are Important Events in Javascript ?

JavaScript Powering a Number