Opens in a new tab
A picture of Jeff Brigman
I CODE TODAY FOR
A BETTER TOMORROW

Glossary Term

Map.prototype.getOrInsert()

2 tags 1 category

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.

The getOrInsert() method is equivalent to the following:

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

Syntax example: getOrInsert(key, defaultValue)

Term Connections

Explore related terms connected by shared tags, shared categories, and relationship phrases. This map loads only when it comes into view.

Loading term connections…