Spec-Zone.ru › JavaScript

Map.prototype.getOrInsert()

Baseline Newly available

This feature works across the latest devices and browser versions. This feature might not work in older devices or browser versions. It's been available across browsers since February 2026.

The getOrInsert() method of Map instances returns the value corresponding to the specified key in this Map. 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 Map.prototype.getOrInsertComputed() instead, which takes a callback to compute the default value only if it's actually needed.

Try it

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

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

Syntax

getOrInsert(key, defaultValue)

Parameters

key
The key of the value to return from the Map object. 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 Map object.

Return value

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

Description

The getOrInsert() method is equivalent to the following:

if (map.has(key)) {
  return map.get(key);
}
map.set(key, defaultValue);
return defaultValue;

It is also similar to the following pattern (which is slightly less reliable if null or undefined are valid values in your map):

map.set(key, map.get(key) ?? defaultValue);

Examples

Multimap

In a map where each key is mapped to an array of values, you can use getOrInsert() to ensure that the array exists for a given key before attempting to push a new value to the array.

map.getOrInsert(key, []).push(value);

Applying default values

You can use getOrInsert() to ensure that a key exists in a map, even if you currently don't need its value. This is usually to normalize user input.

Imagine you have a map of user preferences, and you want to ensure that a certain preference is always set to a default value if the user hasn't specified it:

const options = readConfig();
options.getOrInsert("theme", "light");
options.getOrInsert("fontSize", 14);

// Later in your code, you can safely assume these options exist
document.body.dataset.theme = options.get("theme");

Specifications

Specification
ECMAScript® 2027 Language Specification
# sec-map.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
145
145
144
129
26.2
145
144
96
26.2
No
145
26.2
1.2.20
2.6.7
26.0.0

See also

  • Polyfill of Map.prototype.getOrInsert in core-js
  • es-shims polyfill of Map.prototype.getOrInsert
  • Map
  • Map.prototype.get()
  • Map.prototype.set()
  • Map.prototype.has()
  • Map.prototype.getOrInsertComputed()

© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/getOrInsert

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API