Skip to content

Iterator in a loop is a 'string' instead of 'number' #39089

Description

@andrewgwallace

Version: v12.16.1, v15.14.0 (tested)
Platform(s) (tested):

  • Linux 7b473cfe85f6 5.8.0-1032-gcp fix LICENSE #34~20.04.1-Ubuntu SMP Wed May 19 18:19:35 UTC 2021 x86_64 GNU/Linux
  • Darwin Kernel Version 20.5.0

What steps will reproduce the bug?

Create test array and console log out the typeof of the iterator to see the result

Sample snippet:

const movies = [
  { 
    title: 'Requiem for a Dream',
    duration: 128
  }
]

function iteratorType(array) {
  for (let i in array) {
    console.log(typeof i)
  }
}

iteratorType(movies)

How often does it reproduce? Is there a required condition?

Every time. Conditions: An array with at least one value.

What is the expected behavior?

The iterator should be typeof number.

What do you see instead?

A typeof of 'string' is returned

Activity

  1. andrewgwallace commented on Jun 19, 2021

    @andrewgwallace
    Author

    I guess I'm probably missing some logical reason as to why an iterator is a string but it just seems odd -- When I was writing up a quick sort algorithm, I had to do parseInt(i) in order to make the comparison operator work with the index of the item I was comparing it to. Seems like an unnecessary step.

  2. Trott commented on Jun 19, 2021

    @Trott
    Member

    This is the way JavaScript works and Node.js can't realistically change it. You will get the same result running your code in a browser.

    for...in is not recommended for Arrays generally. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in for explanation, references, and alternatives.

  3. mscdex commented on Jun 19, 2021

    @mscdex
    Contributor

    Option 1: If you don't want to create a new data structure from the existing data, you'll have to use a vanilla for loop (this is the simplest and fastest approach).

    Option 2: You can instead iterate over both the keys and values using movies.entries() instead of just movies and using for-of. Doing so will get you a number type for the index:

    const movies = [
      { 
        title: 'Requiem for a Dream',
        duration: 128
      }
    ]
    
    function iteratorType(array) {
      for (let [i, val] of array.entries()) {
        console.log(typeof i)
      }
    }
    
    iteratorType(movies)

    This option can be handy if you also need the value.

  4. Trott commented on Jun 19, 2021

    @Trott
    Member

    I'm going to close this because there isn't anything we're likely to do to change this behavior. But feel free to leave additional comments/questions if we've missed something.

  5. andrewgwallace commented on Jun 19, 2021

    @andrewgwallace
    Author

    Thank you all for the valuable feedback! ☺️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions