Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Map.prototype.has

Checks whether a key is in a Map.

Available since: ES2015

Syntax

map.has(key)

Parameters

ParameterTypeDescription
keyanyThe key.

Return value

booleantrue if the key is in the Map.

Examples

Checking a key:

const roles = new Map([['anna', 'admin']]);
 
console.log(roles.has('anna'));
console.log(roles.has('oleg'));

Output:

true
false

Notes

has() tells a missing key apart from a key whose value is undefined; get() cannot.

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.