Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Sign InSign Up

Softans

Softans Logo Softans Logo
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
Home/ Questions/Q 1397
In Process
Anonymous
Anonymous
Asked: February 21, 20222022-02-21T06:16:32+00:00 2022-02-21T06:16:32+00:00

For-each over an array in JavaScript

How can I loop through all the entries in an array using JavaScript?

I thought it was something like this:

forEach(instance in theArray)

Where theArray is my array, but this seems to be incorrect.

arrayfor-eachjavascript
  • 0
  • 1 1 Answer
  • 43 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

1 Answer

  • Voted
  • Oldest
  • Recent
  1. Ghulam Nabi
    2022-02-21T06:17:01+00:00Added an answer on February 21, 2022 at 6:17 am

    TL;DR

    • Your best bets are usually
      • a for-of loop (ES2015+ only; spec | MDN) – simple and async-friendly
        for (const element of theArray) {
            // ...use element...
        }
        
      • forEach (ES5+ only; spec | MDN) (or its relatives some and such) – not async-friendly (but see details)
        theArray.forEach(element => {
            // ...use element...
        });
        
      • a simple old-fashioned for loop – async-friendly
        for (let index = 0; index < theArray.length; ++index) {
            const element = theArray[index];
            // ...use element...
        }
        
      • (rarely) for-in with safeguards – async-friendly
        for (const propertyName in theArray) {
            if (/*...is an array element property (see below)...*/) {
                const element = theArray[propertyName];
                // ...use element...
            }
        }
        
    • Some quick “don’t”s:
      • Don’t use for-in unless you use it with safeguards or are at least aware of why it might bite you.
      • Don’t use map if you’re not using its return value.
        (There’s sadly someone out there teaching map [spec / MDN] as though it were forEach — but as I write on my blog, that’s not what it’s for. If you aren’t using the array it creates, don’t use map.)
      • Don’t use forEach if the callback does asynchronous work and you want the forEach to wait until that work is done (because it won’t).

    But there’s lots more to explore, read on…


    JavaScript has powerful semantics for looping through arrays and array-like objects. I’ve split the answer into two parts: Options for genuine arrays, and options for things that are just array-like, such as the arguments object, other iterable objects (ES2015+), DOM collections, and so on.

    Okay, let’s look at our options:

    For Actual Arrays

    You have five options (two supported basically forever, another added by ECMAScript 5 [“ES5”], and two more added in ECMAScript 2015 (“ES2015”, aka “ES6”):

    1. Use for-of (use an iterator implicitly) (ES2015+)
    2. Use forEach and related (ES5+)
    3. Use a simple for loop
    4. Use for-in correctly
    5. Use an iterator explicitly (ES2015+)

    (You can see those old specs here: ES5, ES2015, but both have been superceded; the current editor’s draft is always here.)

    Details:

    1. Use for-of (use an iterator implicitly) (ES2015+)

    ES2015 added iterators and iterables to JavaScript. Arrays are iterable (so are strings, Maps, and Sets, as well as DOM collections and lists, as you’ll see later). Iterable objects provide iterators for their values. The new for-of statement loops through the values returned by an iterator:

     

    const a = ["a", "b", "c"];
    for (const element of a) { // You can use let instead of const if you like
        console.log(element);
    }
    // a
    // b
    // c
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • Popular
  • Answers
  • Ghulam Nabi

    Why are the British confused about us calling bread rolls ...

    • 5 Answers
  • Alex

    application has failed to start because no appropriate graphics hardware ...

    • 4 Answers
  • Jerry

    Add file to native target programmatically via tuist/XcodeProj

    • 4 Answers
  • Ghulam Nabi
    Ghulam Nabi added an answer To resolve the NullPointerException, you need to identify the variable… March 15, 2023 at 8:25 am
  • Ghulam Nabi
    Ghulam Nabi added an answer You can replace the PnP code in your Azure Function… February 13, 2023 at 7:11 am
  • Ghulam Nabi
    Ghulam Nabi added an answer You can use the $match stage in the aggregate pipeline… February 10, 2023 at 6:20 am

Trending Tags

android c++ cypress flutter java javascript python selenium testng webdriver

Top Members

Robert

Robert

  • 3 Questions
  • 1k Points
Luci

Luci

  • 5 Questions
  • 1k Points
Kevin O Brien

Kevin O Brien

  • 2 Questions
  • 1k Points

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Softans

Softans is a social questions & Answers Engine which will help you establish your community and connect with other people.

About Us

  • Blog
  • Jobs
  • About Us
  • Meet The Team
  • Contact Us

Legal Stuff

Help

Follow

© 2021 Softans. All Rights Reserved
With Love by Softans.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.