Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»JavaScript»JavaScript: How to Iterate Over an Array with 4 Different Loops

    JavaScript: How to Iterate Over an Array with 4 Different Loops

    By RahulJanuary 1, 20233 Mins Read

    Iterating over an array is a common task in programming, and there are several ways to do it in JavaScript. In this article, we’ll look at four different methods for looping over an array in JavaScript: the for loop, forEach loop, for-in loop, and for-of loop. We’ll also cover some additional ways to loop over an array using higher-order functions like map and reduce.

    Advertisement

    There are several ways to loop over an array in JavaScript. The most common way is to use a for loop, but there are also other options such as forEach, for-in, and for-of loops.

    Method 1: Using `for` loop

    The for loop is the most traditional way to loop over an array in JavaScript. It has three parts: a starting condition, a stopping condition, and a step.

    To loop over an array with a for loop, you can use the `array.length` property to determine the number of iterations. Here’s an example of how to loop over an array and print each element to the console:

    1
    2
    3
    4
    5
    const days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
     
    for (let i = 0; i < days.length; i++) {
      console.log(days[i]);
    }

    Method 2: Using `forEach` loop

    The forEach loop is a higher-order function that allows you to iterate over an array and perform a callback function on each element. Here’s an example of how to use the forEach loop to print each element of an array to the console:

    1
    2
    3
    4
    5
    const days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
     
    days.forEach(function(day) {
      console.log(day);
    });

    Method 3: Using `for-in` loop

    The for-in loop is used to loop over the properties of an object. You can use the for-in loop to loop over an array by converting it to an object first. Here’s an example of how to do this:

    1
    2
    3
    4
    5
    const days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
     
    for (let day in days) {
      console.log(days[day]);
    }

    Method 4: Using `for-of` loop

    The for-of loop is a newer loop introduced in ES6 that allows you to iterate over the values of an iterable object, such as an array. Here’s an example of how to use the for-of loop to print each element of an array to the console:

    1
    2
    3
    4
    5
    const days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
     
    for (let day of days) {
      console.log(day);
    }

    In addition to the standard looping methods described above, there are also several other ways to loop over an array in JavaScript, such as using the `map` or `reduce` functions or using the `while` or `do-while` loops.

    Conclusion

    In this article, we looked at four different ways to loop over an array in JavaScript: the `for` loop, `forEach` loop, `for-in` loop, and `for-of` loop. We also covered some additional ways to loop over an array using higher-order functions like map and reduce. Whichever method you choose, it’s important to understand the pros and cons of each method and choose the one that best fits your needs. With these techniques in your toolkit, you’ll be well-equipped to iterate over arrays in JavaScript.

    javascript js
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    20 Common JavaScript Interview Questions and Answers

    JavaScript Arrays Tutorial for Beginner's

    JavaScript Arrays: A Beginner’s Guide

    How to Get the Current Timestamp in JavaScript

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Configure Postfix to Use Gmail SMTP on Ubuntu & Debian
    • PHP Arrays: A Beginner’s Guide
    • Deploying Flask Application on Ubuntu (Apache+WSGI)
    • OpenSSL: Working with SSL Certificates, Private Keys and CSRs
    • How to Create and Read List in Python
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.