Skip to content

Confusing inspection for sparse arrays #11570

Description

@TimothyGu
  • Version: all supported versions (oldest tested branch is v4.x (4.8.0))
  • Platform: all
  • Subsystem: util

In the node REPL, sparse elements of an array are shown as "empty":

> new Array(3)
[ , ,  ]

However, when one copy the resulting [ , , ] and reenter it into the prompt, a different array is shown, thus confusing the developer:

> [ , ,  ]
[ ,  ]

How the Chrome inspector deals with this situation is explicitly make the inspected result not compliant JS:

> new Array(3)
[undefined × 3]
> [undefined, , undefined] // to differentiate between a hole and `undefined`
[undefined, undefined × 1, undefined]

Firefox is more upfront:

> new Array(3)
Array [ <3 empty slots> ]
> [undefined, , undefined] // to differentiate between a hole and `undefined`
Array [ undefined, <1 empty slot>, undefined ]

Activity

  1. added
    utilIssues and PRs related to the built-in util module.
    on Feb 26, 2017
  2. bnoordhuis commented on Feb 27, 2017

    @bnoordhuis
    Member

    I agree it's confusing but I think it's correct. Due to the trailing comma, [ , , ] is actually a two-element array, not three:

    > new Array(3).length
    3
    > [ , , ].length
    2
    
  3. TimothyGu commented on Feb 27, 2017

    @TimothyGu
    MemberAuthor

    Yeah, the REPL is doing its job alright. It's just the inspection is a bit counter-intuitive.

    Personally I'm leaning more towards Firefox's approach. It's clear and unambiguous, and I'm worried the Unicode × character used in Chrome's version might not be friendly towards Windows terminals. Substitutes like the letter X might be confusing in themselves.

  4. aqrln commented on Feb 27, 2017

    @aqrln
    Contributor

    @TimothyGu @bnoordhuis what about something like this?

    > new Array(3)
    [ [3 empty] ]
    > [1, 2, , , , 3, 4, 5]
    [ 1, 2, [3 empty], 3, 4, 5 ]
    > [1, 2, , , , 3, 4, 5, , 2]
    [ 1, 2, [3 empty], 3, 4, 5, [1 empty], 2 ]
    
    Screenshot
  5. TimothyGu commented on Feb 27, 2017

    @TimothyGu
    MemberAuthor

    I'd be okay with that. Personally I'd prefer the [3 empty] to have the same color as undefined just to be consistent, though that's bikeshedding.

  6. aqrln commented on Feb 27, 2017

    @aqrln
    Contributor

    @TimothyGu IMO it's more appropriate to mark it with special style, but ¯\_(ツ)_/¯

  7. 36 remaining items

  8. added a commit that references this issue on Jun 3, 2024
  9. added a commit that references this issue on Jul 15, 2024
  10. added a commit that references this issue on Aug 22, 2024
  11. added a commit that references this issue on Oct 2, 2024
  12. added a commit that references this issue on Nov 18, 2024
  13. added a commit that references this issue on Dec 18, 2024
  14. added a commit that references this issue on Jan 20, 2025
  15. added a commit that references this issue on Mar 17, 2025
  16. added a commit that references this issue on Jun 27, 2025
  17. added a commit that references this issue on Aug 21, 2025
  18. added a commit that references this issue on Sep 26, 2025
  19. added a commit that references this issue on Nov 21, 2025
  20. added a commit that references this issue on Feb 24, 2026
  21. added a commit that references this issue on Apr 16, 2026
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

    utilIssues and PRs related to the built-in util module.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions