Home / Web Development / Javascript / How to use Loops in Javascript

How to use Loops in Javascript

Hi Guys,
If you want to run the same code over and over again, each time with a different value. Loops can execute a block of code a number of times.

There are mainly four types of loops in JavaScript.

  • for : loops through a block of code a number of times
  • while : loops through a block of code while a specified condition is true
  • do/while : also loops through a block of code while a specified condition is true

javascript4_loop



Loops are useful when we have to execute the same lines of code repeatedly. For a specific number of times or as long as a specific condition is true

for loop

Syntax

Detail Description

  • statement1: It is executed first even before executing the looping code. So, this statement is normally used to assign values to variables that will be used inside the loop.



  • statement2: It is the condition to execute the loop.
  • statement3:It is executed every time after the looping code is executed.

while loop

The “while loop” is executed as long as the specified condition is true.

while_loop




Syntax

do…while loop

The do…while loop is very similar to while loop.

do_while_loop




Syntax

If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.

Loading Facebook Comments ...
Social Media Auto Publish Powered By : XYZScripts.com