Make delicious recipes!


Loops in Javascript


Javascript supports the following kind of loops:
  1. for
  2. for/in
  3. while
  4. do/while

Examples of the same are as follows:

<script>

var fruits = ["apple", "banana", "grapes"];

// Plain for loop
for (var i=0; i<fruits.length; i++)
    alert(fruits[i]);


// for/in loop iterates over the properties of an object
for (x in fruits)
    alert ("property: " + x + ", value: " + fruits[x]);     


// while loop
var i=0;
while (i<fruits.length)
{
    alert(fruits[i]);
    i++;
}

// Do/while loop
i=0;
do {
    alert(fruits[i]);
    i++;
} while (i<fruits.length)

</script>






Like us on Facebook to remain in touch
with the latest in technology and tutorials!


Got a thought to share or found a
bug in the code?
We'd love to hear from you:

Name:
Email: (Your email is not shared with anybody)
Comment:

Facebook comments:

Site Owner: Sachin Goyal