Full Guide for Loops in JavaScript + Examples

JavaScript Loops

What is a Loop?

A loop is a repeating code block in a program. A loop will execute a code block multiple times until a certain condition is met. There are 3 types of loops in JavaScript: the for loop, the for-in loop, and the while loop.

 

What are JavaScript loops?

JavaScript loops are used to repeatedly run a block of code - usually a block of code that iterates through an array or object.

 

There are a few reasons for using loops in JavaScript:

  • To execute a block of code a set number of times
  • To iterate through arrays or objects
  • To repeat a code block until a certain condition is met

 

Different Kinds of Loops

JavaScript supports different kinds of loops:

  • for - loops through a block of code several times
  • for/in - loops through the properties of an object
  • for/of - loops through the values of an iterable object
  • while - loops through a block of code while a specified condition is true
  • do/while - also 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) {
  // code block to be executed
}

  • 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

for (let i = 0; i < 5; i++) {
  text += "The number is " + i + "<br>";
}

 

The For In Loop

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

Syntax

for (key in object) {
  // code block to be executed
}


Example

const person = {fname:"John", lname:"Doe", age:25};

let text = "";
for (let x in person) {
  text += person[x];
}

 

Example Explained

  • The for in loop iterates over a person object
  • Each iteration returns a key (x)
  • The key is used to access the value of the key
  • The value of the key is person[x]

 


For In Over Arrays

The JavaScript for in statement can also loop over the properties of an Array:

 

Syntax

for (variable in array) {
  code
}

 

Example

const numbers = [45, 4, 9, 16, 25];

let txt = "";
for (let x in numbers) {
  txt += numbers[x];
}

 


The For Of Loop

The JavaScript for of statement loops through the values of an iterable object.

It lets you loop over iterable data structures such as Arrays, Strings, Maps, NodeLists, and more:

 

Syntax

for (variable of iterable) {
  // code block to be executed
}

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.

 


The While Loop

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

 

Syntax

while (condition) {
  // code block to be executed
}

 

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:

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

 

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 {
  // code block to be executed
}
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:

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