I think you’ve heard by now that Array is a type of Object, so I won’t beat that dead horse any further. Well maybe a little.
Which means you can accsss and set properties with names like “Monday” and “Tuesday”. Since it’s a type of Object this is no problem (and a statement like a[‘1’]
is implicitly converting that string ‘1’ to a number 1). But don’t mistake this property name to be an array index, those are specifically integers**. What’s the importance of that?
Arrays have a nice behaviour that when you use an actual array index, the length
property is automatically updated for you. Not to mention array index access is usually optimized by JVM implementations to be noticeably faster than regular property accesses.
Also let’s not forget one of the other huge benefits of having Arrays, they inherit from the Array.prototype, which has a whole lot of useful methods :)!
So in short use Arrays like Arrays — gain the perf boost while also making sure the code is easier to comprehend.
Source: JS Definitive Guide 6th - Array chapter
(** non-negative and less than 2^32-2 since arrays has 32-bit size indexes.)