YogeshChauhan . com

5 Ways to Loop Through JavaScript Arrays

in JavaScript on December 14, 2020

Checkout this post as well:

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
dreamhost

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 uninstall Cocoapods from the Mac OS? #4 How to add Read More Read Less Button using JavaScript? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

May 7 How to disable right click and drag and drop of images using jQuery? May 7 How to render Lists in React? May 7 What’s the difference between variables in CSS and SCSS (Sass)? May 7 How to define variables in SCSS (Sass)? May 7 How to show and hide an element on click in React? May 5 Use inline if to make a shorter conditional syntax in React

You might also like these

How to set the permissions on all files and folders in Mac?MiscHow to draw an SVG path on scroll using JavaScript?JavaScriptWhat is HTMLUnknownElement?HTMLfirst-of-type and last-of-type selectors in CSSCSSHow to Draw a Text Image using JavaScript?JavaScriptForcing the domain to serve securely using HTTPSMisc