Maps and Sets
Missing Map Keys
Provide a fallback when a map lookup is missing.
Missing Map Keys
map_default.js
const item = "milk";
const label = new Map([
["tea", "drink"],
["bread", "food"],
]).get(item) ?? "missing";
console.log("item=" + item);
console.log("label=" + label);
map-default
`Map.get` returns `undefined` when the key is missing. The nullish coalescing operator `??` can turn that missing value into a clear fallback label.