6 Ways to Loop Through an Array in JavaScript

In this article, we discuss 6 Ways to Loop Through an Array in JavaScript

6 Ways to Loop Through an Array in JavaScript

6 Ways to Loop Through an Array in JavaScript
6 Ways to Loop Through an Array in JavaScript

6 Ways to Loop Through an Array in JavaScript

Dealing with arrays is everyday work for every developer. In this article, we are going to see 6 different approaches to how you can iterate through in Javascript.

for Loop

The for loop statement has three expressions:

  1. Initialization – initialize the loop variable with a value and it is executed once
  2. Condition – defines the loop stop condition
  3. Update – executed every time after the code block of the loop has been executed.

Syntax

for (initialization; condition; update statement) {
    // code block to be executed
}

Example

var array = [
    {id: 0, name: 'John', age: 20},
    {id: 1, name: 'Jane', age: 22},
    {id: 2, name: 'Bob', age: 24},
    {id: 3, name: 'Ana', age: 26},
];

for(let i = 0; i < array.length; i++){
    console.log(array[i].name);
}

OUTPUT

John

Jane

Bob

Ana

A break statement can be used to stop the loop at any time.

6 Ways to Loop Through an Array in JavaScript

while Loop

The while loop statement has one expression:

  1. Condition – defines the loop stop condition

Syntax

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

Example

var array = [
    {id: 0, name: 'John', age: 20},
    {id: 1, name: 'Jane', age: 22},
    {id: 2, name: 'Bob', age: 24},
    {id: 3, name: 'Ana', age: 26},
];

var i = 0;
while(i < array.length) {
    console.log(array[i].name)
    i++
}

OUTPUT

John

Jane

Bob

Ana

6 Ways to Loop Through an Array in JavaScript

do/while Loop

The do/while loop statement has one expressions:

  1. Condition – defines the loop stop condition

Syntax

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

Example

var array = [
    {id: 0, name: 'John', age: 20},
    {id: 1, name: 'Jane', age: 22},
    {id: 2, name: 'Bob', age: 24},
    {id: 3, name: 'Ana', age: 26},
];

var i = 0;
do {
    console.log(array[i].name)
    i++
} while(i < array.length)
 

OUTPUT

John

Jane

Bob

Ana

6 Ways to Loop Through an Array in JavaScript

for/of Loop

The for/of loop statement has two expressions:

  1. Iterator – refers to the array who will be iterated
  2. Variable – The value of the next iteration stored in a variable (which has to be declared with either const, let, or var to hold the value)

Syntax

for (value of iterator) {
    // code block to be executed
}

Example

var array = [
    {id: 0, name: 'John', age: 20},
    {id: 1, name: 'Jane', age: 22},
    {id: 2, name: 'Bob', age: 24},
    {id: 3, name: 'Ana', age: 26},
];

for (let index of array) {
    console.log(index.name)
}

OUTPUT

John

Jane

Bob

Ana

6 Ways to Loop Through an Array in JavaScript

forEach()

The forEach() is a method of the array that uses a callback function to include any custom logic to the iteration. The forEach() will execute the provided callback function once for each array element.

The callback function takes up 3 arguments:

  1. currentValue – value of the current element
  2. index (optional) – array index of the current element
  3. array (optional) – entire array object

The thisArg (optional) – value for the callback function which will be used as its this value.

Syntax

array.forEach(callback(currentValue [, index [, array]])[, thisArg]);

Example 

var array = [
    {id: 0, name: 'John', age: 20},
    {id: 1, name: 'Jane', age: 22},
    {id: 2, name: 'Bob', age: 24},
    {id: 3, name: 'Ana', age: 26},
];

array.forEach(function(profile, index, myArray) {
    console.log(`Index: ${index}, Name: ${profile.name}`);
});

OUTPUT

John

Jane

Bob

Ana

map()

The map() method is very similar to the forEach() method as it will also execute the provided callback function for each element in the array. The difference between the two is that the map() method creates and returns a new array based on the result of the provided callback function.

The callback function takes up 3 arguments:

  1. currentValue – value of the current element
  2. index (optional) – array index of the current element
  3. array (optional) – entire array object

The thisArg (optional) – value for the callback function which will be used as its this value.

Syntax

var new_array = array.map(function callback(currentValue[, index[, array]]) {
    // Return element for new_array
}[, thisArg])

Example

var array = [
    {id: 0, name: 'John', age: 20},
    {id: 1, name: 'Jane', age: 22},
    {id: 2, name: 'Bob', age: 24},
    {id: 3, name: 'Ana', age: 26},
];

var newArray = myArray.map(function(profile, index, myArr) {
    var newProfile = {
        'id': index,
        'name': profile.firstName,
        'age': profile.age
    }

    return newProfile
})

newArray.forEach(function(profile, index, myArr) {
    console.log(profile.fullName)
});

OUTPUT

John

Jane

Bob

Ana

we have come to the end of our article. I hope you understood 6 Ways to Loop Through an Array in JavaScript

Also Check out What are the different types of operators in JavaScript 2021

If you interested in Entertainment genre please check this also Deepika Padukone Age, Biography, Wiki, Family, Filmography & More

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

How to Protect yourself from CovidVaccination saves lives at every stage of life
  • Clean your home regularly, particularly frequently touched surfaces like
  • Stay home stay safe. Follow the Golden Rule. Wash your hands frequently with soap and water or use alchohol based hand-rub.
  • Stay home stay safe. Stay physically fit. Exercise regulary. Eat a nutritious diet. Don’t smoke.
  • Stay home stay safe. If you show symptoms of COVID-19, self isolate yourself, wear a mask around others and seek medical advice