Map.prototype.has
Checks whether a key is in a Map.
Available since: ES2015
Syntax
map.has(key)Parameters
| Parameter | Type | Description |
|---|---|---|
| key | any | The 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
falseNotes
has() tells a missing key apart from a key whose value is undefined; get() cannot.
See also
- Map.prototype.get — Returns the value for a key.
- Set.prototype.has — Checks whether a value is in a Set.
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.