Javascript loop

tutorial and example
tutorial and example

The way of repetition will form a circle that’s why repetition statements are called loops. In some situations we use Loop statements to reduce the number of lines. It is mostly used in Array.

There are 3 types of loops in JavaScript.

  1. while loop
  2. for loop
  3. do-while loop

while loop

In while loop the code is executed, until the expression is true. It should be used if number of iteration is not known.

Syntax

Example

Try Now

Output

For loop

In for loop itinitialized the value then check condition and then increment or decrement occurred.

It execute at fixed number of times.

Syntax

Example

Try Now

Output

do while loop

Do while loop executes elements for infinite number of times same as like while loop, but code is executed once whether the condition is true or false. When we need to repeat the statement block at least once then we use do-while.

Syntax

Example

Try Now

Output