Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Map.prototype.get

Returns the value for a key.

Available since: ES2015

Syntax

map.get(key)

Parameters

ParameterTypeDescription
keyanyThe key.

Return value

anyThe value, or undefined if the key is missing.

Examples

An existing and a missing key:

const capitals = new Map([['France', 'Paris']]);
 
console.log(capitals.get('France'));
console.log(capitals.get('Spain'));

Output:

Paris
undefined

A default value:

const capitals = new Map([['France', 'Paris']]);
 
console.log(capitals.get('Spain') ?? 'unknown');

Output:

unknown

Notes

Square brackets do not work with a Map: capitals['France'] returns undefined. Always use get().

See also

In the tutorial

  • Map and Set — The Map and Set collections in JavaScript: a dictionary with keys of any type and a set of unique values.

Thanks for reading!

Donate to the site — we work hard for you.