Yogesh Chauhan's Blog

5 Ways to Loop Through JavaScript Arrays

in JavaScript on December 14, 2020

Similar post:

Quick Introduction To 11 Array Iteration Methods In JavaScript

1. For Loop

The safest way to loop through an array, is using a for loop:


var languages = [
    {id: 1, name: 'PHP', ratings: 8},
    {id: 2, name: 'Python', ratings: 9},
    {id: 3, name: 'C', ratings: 8},
    {id: 4, name: 'Go', ratings: 8},
    {id: 5, name: 'Swift', ratings: 7}
];

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

// output
PHP
Python
C
Go
Swift

2. for/of Loop


var languages = [
    {id: 1, name: 'PHP', ratings: 8},
    {id: 2, name: 'Python', ratings: 9},
    {id: 3, name: 'C', ratings: 8},
    {id: 4, name: 'Go', ratings: 8},
    {id: 5, name: 'Swift', ratings: 7}
];

for (let lang of languages) {
    console.log(lang.name)
}

// output
PHP
Python
C
Go
Swift

3. forEach()


var languages = [
    {id: 1, name: 'PHP', ratings: 8},
    {id: 2, name: 'Python', ratings: 9},
    {id: 3, name: 'C', ratings: 8},
    {id: 4, name: 'Go', ratings: 8},
    {id: 5, name: 'Swift', ratings: 7}
];

languages.forEach(function(lang, i) {
    console.log(`Id: ${i}, Name: ${lang.name}`);
});

// output
Id: 0, Name: PHP
Id: 1, Name: Python
Id: 2, Name: C
Id: 3, Name: Go
Id: 4, Name: Swift

4. while Loop


var languages = [
    {id: 1, name: 'PHP', ratings: 8},
    {id: 2, name: 'Python', ratings: 9},
    {id: 3, name: 'C', ratings: 8},
    {id: 4, name: 'Go', ratings: 8},
    {id: 5, name: 'Swift', ratings: 7}
];

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

// output
PHP
Python
C
Go
Swift

5. do/while Loop


var languages = [
    {id: 1, name: 'PHP', ratings: 8},
    {id: 2, name: 'Python', ratings: 9},
    {id: 3, name: 'C', ratings: 8},
    {id: 4, name: 'Go', ratings: 8},
    {id: 5, name: 'Swift', ratings: 7}
];

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

// output
PHP
Python
C
Go
Swift

Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
The Lending Club Analysis using Logistic Regression and Random Forest in RStudioMiscellaneousHow to change value of a span tag using a reference from another div using jQuery?jQueryAlternate Style Sheets in CSSCSSCreate a currency (dollar) to coins convertor in ReactReactCanvas Drawing in HTML5HTMLDifferent Types of Functions in JavaScriptJavaScript