JS: Map.prototype.getOrInsert
(new in ECMAScript 2026)
mapObj.get(key, newVal)-
get the value of the key, if not exist, insert it and return the value.
const xx = new Map([[4, "n4"], [5, "n5"]]); // get the value of a existing key console.assert(xx.getOrInsert(5, "n5") === "n5"); // get the value of a key if non-exist, insert it with a value console.assert(xx.getOrInsert(6, "n6") === "n6"); console.assert(xx.getOrInsert(6) === "n6");
JS Map.prototype
- JS: Map.prototype.constructor
- JS: Map.prototype.size
- JS: Map.prototype[Symbol.toStringTag]
- JS: Map.prototype.get
- JS: Map.prototype.getOrInsert
- JS: Map.prototype.getOrInsertComputed
- JS: Map.prototype.set
- JS: Map.prototype.has
- JS: Map.prototype.delete
- JS: Map.prototype.clear
- JS: Map.prototype.forEach
- JS: Map.prototype.keys
- JS: Map.prototype.values
- JS: Map.prototype.entries