Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Map.prototype.has: test for a key

The has method returns a boolean indicating whether a Map contains an entry for the supplied key.

Version: ECMAScript 2015 onward; example target: Node.js 24.22.2

Syntax

map.has(key)

map.has(key) performs no key coercion and does not mutate the collection. It returns true for an existing entry even when its value is undefined, otherwise false.

Keys use SameValueZero: NaN matches NaN, -0 matches +0, and objects match only by identical reference. Thus has distinguishes absence from get returning undefined.

An incompatible receiver throws TypeError. The method has been standard since ECMAScript 2015; testing membership neither reads nor copies the stored value.

Examples

const key = {};
const map = new Map([[key, undefined], [NaN, "nan"], [-0, "zero"]]);
console.log(map.has(key), map.has({}));
console.log(map.has(NaN), map.has(+0));
console.log(map.get(key), map.has(key));
try { Map.prototype.has.call({}, "x"); } catch (error) { console.log(error instanceof TypeError); }

Result

true false
true true
undefined true
true

Sources