Different Loops in JavaScript

Here are the different types of loops in JavaScript:

  1. For Loop:
    The for loop is used to iterate over a block of code a specific number of times. It consists of three parts: initialization, condition, and increment.

Example:

for (let i = 1; i <= 5; i++) {
  console.log(i);
}
/*
1
2
3
4
5
*/

This loop will print the numbers 1 to 5 in the console.

  1. While Loop:
    The while loop is used to repeatedly execute a block of code as long as a condition is true. It checks the condition before each iteration.

Example:

let count = 1;
while (count <= 5) {
  console.log(count);
  count++;
}
/*
1
2
3
4
5
*/

This loop will print the numbers 1 to 5 in the console.

  1. Do-While Loop:
    Do-while loop executes the block of code inside “do” at least once before checking the condition defined in “while”. so simply you can say do this while this is true and do this at least once. After executing the code block once it checks the condition if it is true then it executes the code block again and then check the while condition again , loop runs until while condition becomes false.

Example:

let count = 1;
do {
  console.log(count);
  count++;
} while (count <= 5);
/*
1
2
3
4
5
*/

This loop will also print the numbers 1 to 5 in the console.

  1. For…in Loop:
    The for...in loop is used to iterate over the properties of an object. It retrieves the keys of an object.

Example:

const person = {
  name: 'Sam',
  age: 30,
  city: 'New York'
};
for (let key in person) {
  console.log(key + ': ' + person[key]);
}
/* 
name: Sam
age: 30
city: New York 
*/

This loop will print the properties and their values of the person object, such as name: Sam, age: 30, and city: New York.

  1. For…of Loop:
    The for...of loop is used to iterate over elements of an iterable object, such as arrays or strings.

Example:

const fruits = ['apple', 'banana', 'orange'];
for (let fruit of fruits) {
  console.log(fruit);
}
/*
apple
banana
orange
*/

This loop will print each fruit in the fruits array, such as apple, banana, and orange.

These are the main types of loops in JavaScript. They provide different ways to control the flow of execution and repeat code based on specific conditions.

  1. For…of Loop (Iterating Strings):
    The for...of loop can also be used to iterate over individual characters in a string.

Example:

const message = 'Hello, world!';
for (let char of message) {
  console.log(char);
}
/*
H
e
l
l
o
,
w
o
r
l
d
!
*/

This loop will print each character in the message string.

  1. For…in Loop (Iterating Arrays):
    Although primarily used for objects, the for...in loop can also be used to iterate over the indices of an array.

Example:

const numbers = [1, 2, 3, 4, 5];
for (let index in numbers) {
  console.log(index + ': ' + numbers[index]);
}
/*
0: 1
1: 2
2: 3
3: 4
4: 5
*/

This loop will print the indices and corresponding values of the numbers array, such as 0: 1, 1: 2, and so on.

  1. forEach Loop (Array Method):
    The forEach loop is an array method that executes a provided function once for each element in an array.

Example:

const colors = ['red', 'green', 'blue'];
colors.forEach((color) => {
  console.log(color);
});
/*
red
green
blue
*/

This loop will print each color in the colors array.

Don't Miss Out! Subscribe to Read Our Latest Blogs.

Subscription form (#5)

If you found this blog helpful, share it on social media.

Pin It on Pinterest

Scroll to Top