A map (noun) is a data structure that associates values (the keys) with other values. Object is similar to map, both let you set keys to values, retrieve those values, delete keys, and detect whether something is stored at a key. For this reason (and because there were no built-in alternatives), Object has been used as map historically.
let ages = {
Boris: 39,
Liang: 22,
Julia: 62,
}
console.log(`Julia is ${ages["Julia"]}`) // 62
console.log("Is Jack's age known?", "Jack" in ages); // false
console.log("Is toString's age known?", "toString" in ages); // true
As of ES5, this can be avoided by using Object.create(null), but this is
seldom done. Another strategy is checking for the key in Object.keys which
returns the object's own keys without those in the prototype. However, a
built-in was added for maps:
let ages = new Map()
ages.set("Boris", 39)
ages.set("Liang", 22)
ages.set("Julia", 62)
console.log(`Julia is ${ages.get("Julia")}`) // 62
console.log("Is Jack's age known?", ages.has("Jack")); // false
console.log("Is toString's age known?", ages.has("toString")); // false
size
Maps have a size property that tells you how many keys are stored in them.