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 are Asynchronous Scripts in Javascript ?

What are Different Error Types in Javascript ?

JavaScript Powering a Number

What are Different Javascript Frameworks ?

What is go Method of History Object in Javascript ?