WebTrainingRoom Online Courses
Learn JavaScript, Free JavaScript Tutorial for Beginners, Interactive JavaScript Examples

Javascript Loop, for loop, do while loop

JavaScript Loops

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 example in JavaScript

JavaScript For Loop Example

this is how to write ForLoop in JavaScript

<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 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
 
Loop in Javascript, for loop, do while loop, Learn JavaScript Syntax

JavaScript Tutorial


Consulting