-1

From my readings, in JavaScript:

Objects = Hash Tables, which are build on Arrays. However, it is commonly said that Arrays are Objects in JS. How are these two concepts reconciled?

4
  • A 10 second google gives: eloquentjavascript.net/04_data.html, and metaltoad.com/blog/… Commented Nov 19, 2018 at 11:09
  • 4
    Objects are not built on arrays - it's the other way around. Commented Nov 19, 2018 at 11:09
  • Maybe you mean this: typeof []; //returns object or this new String() I would rather say everything in JS is build on objects. Commented Nov 19, 2018 at 11:17
  • myArr = ['this', 'is', 'an', 'array'] is actually { 0: 'this', 1: 'is', 2: 'an', 3: 'array' } behind the scenes. Commented Nov 19, 2018 at 11:23

1 Answer 1

0

Objects are not built on arrays. Objects have their own optimizations.

In general:

  • Objects are for "structs", structures of predictable "shape" and keys known in advance (even though they can be used with dynamic keys, you should use Maps for that. See below).
  • Arrays are for lists (and queues, and stacks), structures where the keys are numbers, or where the order of elements matters. Arrays are "special" objects, not the other way around. (You can put string-based properties on an array, just like any object. Please don't do that though).
  • Maps are for hash tables/dictionaries, structures where the keys are dynamic and not known in advance.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.