JavaScript Loops

JavaScript Tutorial

What Are JavaScript Loops?

JavaScript Loops are used to repeat a block of code multiple times until a condition becomes false.

Example use cases:

 Types of Loops in Js

  1. for

  2. while

  3. do...while

  4. for...in

  5. for...of

for Loop

Used when you know how many times you want to repeat.

 Output:

1
2
3
4
5

while Loop

Runs as long as the condition is true.


 

do…while Loop

Runs the code at least once, even if the condition is false.


 

for…in Loop

Used to loop through objects.


 

Output:

name: John
age: 25
city: Delhi

for…of Loop

Used to loop through arrays or strings.


 

Output:

red
green
blue

 Example with HTML Output


 

Output:

Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

Summary Table

Loop TypeBest Use
forKnown number of repetitions
whileRepeat until condition changes
do...whileRun at least once
for...inLoop through object keys
for...ofLoop through array or string values

You may also like...