Categories
JavaScript Web development

JavaScript Loops

JavaScript Loops are handy, if you want to run the same code over and over again, each time with a different value. Loops can execute a block of code a number of times.

JavaScript Loops

Often this is the case when working with arrays:

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
var cars = ["Mini", "BMW", "Opel", "Ford", "Fiat", "Audi"];
var text = "";
var i;
for (i = 0; i < cars.length; i++) {
  text += cars[i] + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

Output:

Different Kinds of Loops

JavaScript supports different kinds of loops:

forloops through a block of code a number of times
for/inloops through the properties of an object
for/ofloops through the values of an iterable object 
whileloops through a block of code while a specified condition is true
do/whilealso loops through a block of code while a specified condition is true

The For Loop

The for loop has the following syntax:

for (statement 1; statement 2; statement 3) {
}

Statement 1 is executed (one time) before the execution of the code block.

Statement 2 defines the condition for executing the code block.

Statement 3 is executed (every time) after the code block has been executed.

Example:

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
var text = "";
var i;
for (i = 0; i < 5; i++) {
  text += "The number is " + i + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

Output:

From the example above, you can read:
Statement 1 sets a variable before the loop starts (var i = 0).
Statement 2 defines the condition for the loop to run (i must be less than 5).
Statement 3 increases a value (i++) each time the code block in the loop has been executed.

The For/In Loop

The JavaScript for/in statement loops through the properties of an object:

<!DOCTYPE html>
<html>
<body>


<p>The for/in statement loops through the properties of an object.</p>

<p id="demo"></p>

<script>
var txt = "";
var person = {fname:"Adam", lname:"Smith", age:48}; 
var x;
for (x in person) {
  txt += person[x] + " ";
}
document.getElementById("demo").innerHTML = txt;
</script>

</body>
</html>

Output:

The For/Of Loop

The JavaScript for/of statement loops through the values of an iterable objects

for/of lets you loop over data structures that are iterable such as Arrays, Strings, Maps, NodeLists, and more.

The for/of loop has the following syntax:

for (variable of iterable) {
}

variable– For every iteration the value of the next property is assigned to the variable. Variable- can be declared with: const, let, or var.

iterable– An object that has iterable properties.

Looping over an Array

<!DOCTYPE html>
<html>
<body>

<p>The for/of statement loops through the values of an iterable object.</p>

<script>
var cars = ['BMW', 'Audi', 'Mini'];
var x;

for (x of cars) {
  document.write(x + "<br >");
}
</script>

</body>
</html>

Output:

Looping over a String

<!DOCTYPE html>
<html>
<body>

<p>The for/of statement loops through the values of an iterable object.</p>

<script>
var txt = 'JavaScript';
var x;

for (x of txt) {
  document.write(x + "<br >");
}
</script>

</body>
</html>

Output:

The While Loop

The while loop loops through a block of code as long as a specified condition is true.

Syntax:

while (condition) {
}

Example:

In the following example, the code in the loop will run, over and over again, as long as a variable (i) is less than 10:

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
var text = "";
var i = 0;
while (i < 10) {
  text += "<br>The number is " + i;
  i++;
}
document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

Output:

The Do/While Loop

The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

Syntax:

do {
}
while (condition);

Example:

The example below uses a do/while loop. The loop will always be executed at least once, even if the condition is false, because the code block is executed before the condition is tested:

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
var text = ""
var i = 0;

do {
  text += "<br>The number is " + i;
  i++;
}
while (i < 10);  

document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

Output:

Do not forget to increase the variable used in the condition, otherwise the loop will never end!

Enjoy coding!