Free Web Development Online Tutorials, Learn to Code
Learn JavaScript Loop, for loop example, javascript loop through array object, javascript while loop example

Javascript loop example- for loop- while loop

Learn how to write loop in JavaScript, loop is very useful when you want to perform same task repeatedly with different parameter for any collection, in JavaScript we can loop through array or any dictionary object.

Loop in JavaScript

When you need same set of code to be executed repeatedly with some different values, then Loop is the perfect solution. Here we see different type of Loop in JavaScript example

You will be learning following points:

  • JavaScript For Loop Example
  • javascript loop through array
  • Array methods for iteration
  • JavaScript Do While Loop Example
  • JavaScript While Loop Example

JavaScript For Loop Example

this is how to write For Loop in JavaScript

for ([start]; [condition]; [final-expression])
        statement
<script>
for (i = 0; i <= 5; i++)
{
    document.write("Javascript for loop : value of i " + i + " <br>");
}
</script>
//Result

This will print the value of i every time, so you can write your code block inside the loop that you want to be executed again and again until the condition is satisfied (here in example : i <= 10)

javascript loop through array

here is an example of javascript loop through array of objects

var arr = ["red", "blue", "color", "Green", "orange"];
for (var i = 0; i < arr.length; i++) {
        document.writeln(arr[i]);
        document.writeln("<br>");
    }

Result:


javascript for loop break example with conditional logic

in following example I am checking if the value is "color" then break the loop.

var arr = ["red", "blue", "color", "Green", "orange"];
for (var i = 0; i < arr.length; i++) {
    document.writeln(arr[i]);
    if (arr[i] == "color")
        break;
}

Result:


Array methods for iteration

Iterate over the elements in an array
Note: JavaScript forEach does not support break; you can use every() method instead

var arrColor = ["red", "blue", "color", "Green", "orange"];
arrColor.forEach(function (elem) {
    document.writeln(elem);
});

Result:


Iterate using every() method

var arrColor = ["red", "blue", "color", "Green", "orange"];
arrColor.every(function (elem) {
    document.writeln(elem);
     return true; // must;
});

Result:


In above example if you want to add a condition to break , use the code below

     if (elem.length === 0) {
                return false; // break
            }

JavaScript Do While Loop Example

Do Loop will execute first and While condition is satisfied it will exit.

<script>
var i = 0;
do {
document.write("Javascript do while loop: value of i " + i + "<br/>");
i++;
}
while (i <= 5);
</script>
//Result

JavaScript While Loop Example

While loop will continue until condition is satisfied

<script>
var i = 0;
while (i < 5) {
        document.write("Javascript While loop: value of i  " + i + "<br/>");
    i++;
    }
</script>
// Result

Loop syntax are very similar in others language too, take a look at loop in other languages

Comment
Name
Email
Website
Subscribe
 
javaScript loops examples


javascript Interview Questions Answers


JavaScript Examples | JavaScript Online Course