Loops in JavaScript

Javascript uses three types of the loop:

For loop:

Example:

<script type="text/javascript">

               var name = new Array("Naveen", "Jitendra");

               document.write("<b>Using for loops </b><br />");

               for (i=0;i<name.length;i++)

                     {

                             document.write(name[i] + "<br />");

                     }

</script>

While loop:

<script type="text/javascript">

               document.write("<b>while loops </b><br />");

               var i = 0, j = 1, k;

               document.write("Fibonacci series less than 40<br />");

               while(i<40)

               {

                       document.write(i + "<br />");

                       k = i+j;

                       i = j;

                       j = k;

               }

</script>

do…while loop

<script type="text/javascript">

                        document.write("<b>do...while loops </b><br />");

                        var i = 2;

                        document.write("Even numbers less than 20<br />");

                        do

                        {

                                    document.write(i + "<br />");

                                    i = i + 2;

                        }while(i<20)

</script>

Powered  by Codersarts