map.getOrInsert(key, defaultValue) returns the value stored for key. If the key is missing, it first stores defaultValue and returns it. It replaces if (!map.has(k)) map.set(k, v); return map.get(k);. It is newer, added to Map and WeakMap together, and not in every engine yet: check before use.
Remember
map.getOrInsert(key, defaultValue)
key is stored → returns the stored value (default ignored)
key is missing → stores defaultValue, then returns it
Syntax
Call getOrInsert with a key and a default for when the key is missing.
JavaScript
map.getOrInsert(key, defaultValue)
Call
What it does
map.getOrInsert(key, defaultValue)
Return the value for key, or store defaultValue first if the key is missing.
typeof Map.prototype.getOrInsert === "function"
Feature check. Run it before relying on the method.
Newer method, not in every engine yet: Node.js 24.15 lacks it in our test, Chrome 148 has it. Each example starts with a short fallback. For a costly default, use getOrInsertComputed().
Library declarations
JavaScript
map.getOrInsert(key, defaultValue) // returns the stored or the new value
// key: any value (SameValueZero)
// defaultValue: used only if key is missing
Parameters
Parameter
Description
key
Required. Any value. Matched with SameValueZero, so NaN finds a NaN key.
defaultValue
Required. Stored only when key is missing. Ignored when the key already has a value.
Return Value
Return Value
Description
any value
The value already stored for the key, or defaultValue after it has been stored.
JavaScript Examples
Example 1 — Read the Value, or Store a Default
The first call stores the default. Later calls return the stored value and ignore the new default.
JavaScript
// Fills in getOrInsert only on engines that lack it.
if (!Map.prototype.getOrInsert) {
Object.defineProperty(Map.prototype, "getOrInsert", {
configurable: true, writable: true,
value(k, d) { if (!this.has(k)) this.set(k, d); return this.get(k); }
});
}
const roles = new Map();
console.log(roles.has("ann")); // false
console.log(roles.getOrInsert("ann", "guest")); // guest
console.log(roles.has("ann")); // true
console.log(roles.getOrInsert("ann", "admin")); // guest
One line does three jobs.getOrInsert(id, { count: 0 }) stores a fresh counter if needed, and returns it.
The same object comes back. The stored object comes back next time, so entry.count += 1 updates it.
Each key has a counter."cancel" is another key, so it starts at 0 and leaves the "save" counter alone.
Example 3 — The Default Is Built Every Time
A helper counts how often the default is built, even when the key is already there.
JavaScript
// Fills in getOrInsert only on engines that lack it.
if (!Map.prototype.getOrInsert) {
Object.defineProperty(Map.prototype, "getOrInsert", {
configurable: true, writable: true,
value(k, d) { if (!this.has(k)) this.set(k, d); return this.get(k); }
});
}
let created = 0;
function makeList() {
created += 1;
return [];
}
const groups = new Map();
groups.getOrInsert("team", makeList()).push("Ann");
groups.getOrInsert("team", makeList()).push("Bob");
console.log(JSON.stringify(groups.get("team"))); // ["Ann","Bob"]
console.log(created); // 2