WeakMap.prototype.getOrInsert()

Baseline 2026
Newly available

Since February 2026, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

The getOrInsert() method of WeakMap instances returns the value corresponding to the specified key in this WeakMap. If the key is not present, it inserts a new entry with the key and a given default value, and returns the inserted value.

If the computation of the default value is expensive, consider using WeakMap.prototype.getOrInsertComputed() instead, which takes a callback to compute the default value only if it's actually needed.

Try it

const map = new WeakMap([[window, "foo"]]);
console.log(map.getOrInsert(window, "default"));
// Expected output: "foo"

console.log(map.getOrInsert({}, "default"));
// Expected output: "default"

Syntax

js
getOrInsert(key, defaultValue)

Parameters

key

The key of the value to return from the WeakMap object. Must be either an object or a non-registered symbol. Object keys are compared by reference, not by value.

defaultValue

The value to insert and return if the key is not already present in the WeakMap object.

Return value

The value associated with the specified key in the WeakMap object. If the key can't be found, defaultValue is inserted and returned.

Exceptions

TypeError

Thrown if key is not an object or a non-registered symbol.

Examples

Using getOrInsert()

js
const wm = new WeakMap();
const obj = {};

console.log(wm.get(obj)); // undefined
console.log(wm.getOrInsert(obj, "default")); // "default"
console.log(wm.get(obj)); // "default"
console.log(wm.getOrInsert(obj, "another default")); // "default"

Specifications

Specification
ECMAScript® 2027 Language Specification
# sec-weakmap.prototype.getorinsert

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Bun
Deno
Node.js
getOrInsert
Chrome – Full support
Chrome 145 (Release date: 2026-02-10)
footnote Full support
Edge – Full support
Edge 145 (Release date: 2026-02-14)
footnote Full support
Firefox – Full support
Firefox 144 (Release date: 2025-10-14)
footnote Full support
Opera – Full support
Opera 129 (Release date: 2026-03-18)
footnote Full support
Safari – Full support
Safari 26.2 (Release date: 2025-12-12)
footnote Full support
Chrome Android – Full support
Chrome Android 145 (Release date: 2026-02-10)
footnote Full support
Firefox for Android – Full support
Firefox for Android 144 (Release date: 2025-10-14)
footnote Full support
Opera Android – Full support
Opera Android 96 (Release date: 2026-03-10)
footnote Full support
Safari on iOS – Full support
Safari on iOS 26.2 (Release date: 2025-12-12)
footnote Full support
Samsung Internet – No support
Samsung Internet
footnote No support
WebView Android – Full support
WebView Android 145 (Release date: 2026-02-10)
footnote Full support
WebView on iOS – Full support
WebView on iOS 26.2 (Release date: 2025-12-12)
footnote Full support
Bun – Full support
Bun 1.2.20 (Release date: 2025-08-10)
footnote Full support
Deno – No support
Deno
footnote No support
Node.js – No support
Node.js
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support

See also