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
Give the gift of Audible

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 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
What are Modules and Components in Angular?AngularHow to CREATE TABLE in SQL with and without using Another Table?SQL/MySQLWhat is a Strict Requirement in PHP 7 Function Declarations?PHPHow to modify navigation menus in WordPress?WordPressA quick introduction to API, REST API and PostmanMiscellaneousFor Each Loop in Swift for BeginnersSwift