#code #javascript
The correct way to add values to a Map
in JavaScript is with the map.set(key, value)
function. Using the map[key]=value
notation seems to work at first, but the key-value pair is ignored by methods on the Map, like map.has(key)
and map.delete(keys)
.
Maps and Objects are not the same thing. They are similar In the fact that they are both key-value pairs. But there are notable differences : - an Object contains values by default (prototype) - an Object is vulnerable to object injection attacks, so a Map is safer - any value can be used as key in a Map. For Objects, only String and Symbols are allowed - Map is iterable and the keys are ordered in the order they were created - Map has a size property - Map has better performances when adding & removing many keys
Map
was added with ES6 (2015) and is not supported by all browsers.