Yup, {}
is an empty object and []
is an empty array. Note that an array is a kind of object, optimized to handle a list of values, but, like any Javascript object, it can accept other attributes like "one"
or "two"
— which is how it can support methods like myArray.push(1)
: push
is an attribute of all arrays. In fact, you can even say myArray["push"] = someOtherFunction
to override push
without any trouble. Arrays, like all Javascript objects, support arbitrary key-value assignments.
Ultimately, though, it has to do with behind-the-scenes performance. If you're looking to store a sequential list of values, the array will handle it much better and offer some helpful attributes like push
, shift
, length
, etc. — plus, future developers will actually know what you're doing. If you're looking to just store key-value pairs, the object is the way to go, since it's not bogged down by all that extra weight.
In short, while they both support key-value pairs, they're very different behind the scenes. Don't worry too much about it and use whichever lends itself better to the job at hand.