You are currently viewing Types of Loops in JavaScript | Explained

Types of Loops in JavaScript | Explained

Types of Loops in Javascript

In this article, we will see about types of loops in javascript.

Different types of loops

There are three types of loops available in Javascript:

  • Linear loop
  • Quadratic loop
  • Cubic loop

Linear loop

Code:

for(var i=1;i<=3;i++){
document.write("hello");
}

Output:
hellohellohello

Quadratic loop:

Inner loop iterations depend upon the outer loop counter variable value

Code:

for(var i=1;i<=3;i++){
for(var j=1;j<=2;j++){
document.write("hello");
}
document.write("<br>")
}

Output:
hellohello
hellohello
hellohello

Cubic loop:

Code:

for(var i=1;i<=3;i++){
     for(var j=1;j<=2;j++){
             for(var k=1;k<=1;k++){
document.write("Welcome <br>"); //nxnxn times
}}}

Output:
Welcome
Welcome
Welcome
Welcome
Welcome
Welcome

Leave a Reply