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

JavaScript How To Find The Average Of 10 Numbers Using A While Lo ...

What are Advantages of Javascript ?

What is Prototype in Javascript ?

What is go Method of History Object in Javascript ?

What is Difference Between Call and Apply Methods in Javascript ?