-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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
lightspeed
  • 313
  • 4
  • 11
  • A 10 second google gives: https://eloquentjavascript.net/04_data.html, and https://www.metaltoad.com/blog/javascript-understanding-objects-vs-arrays-and-when-use-them-part-1 – Stuart Nov 19 '18 at 11:09
  • 4
    Objects are *not* built on arrays - it's the other way around. – CertainPerformance Nov 19 '18 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. – Zydnar Nov 19 '18 at 11:17
  • `myArr = ['this', 'is', 'an', 'array']` is actually `{ 0: 'this', 1: 'is', 2: 'an', 3: 'array' }` behind the scenes. – AnonymousSB Nov 19 '18 at 11:23

1 Answers1

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.
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308