Member-only story
Stop Using Objects as Hash Maps in JavaScript
There’s a better way to do that
A map is one of the most frequently used data structures in daily programming. It keeps key-value pairs that can easily be accessed by their keys. In Java, it is pretty obvious to use a HashMap for that. However, in JavaScript, it is quite convenient to just use a plain object for that purpose:
But there is a built-in data structure in JavaScript that was made for exactly that purpose: Map. Let me give you some reasons to prefer Map over plain objects.
1. More Key Types
Objects can only have symbols or strings. Maps can have any type of value as a key: objects, functions, or primitives.