Map.prototype.has()

Reports whether the map contains an entry for the given key, returning true or false.

Signature
has(key)
  • Added in JavaScript ES2015

Parameters

ParameterTypeDescription
$keyanyThe key of the entry to test for presence. Object keys are compared by reference, not by value.

Return value

booleantrue if an entry with the specified key exists in the Map, otherwise false.

Errors

Throws a TypeError when called on a value that is not a real Map.

Changelog

  • PHP ES2015Introduced with the Map built-in.

has() answers presence, not value. It is the reason a Map is safe for user-supplied keys: an empty Map returns false for "toString", "constructor" and "__proto__", whereas the in operator on a plain object walks the prototype chain and answers true.

When you actually need it

Most lookups can just call get() and check the result. Reach for has() when undefined is a legitimate stored value — a memoization cache, a parsed config where a key may be explicitly blank, a set-like Map. Otherwise calling has() and then get() does the same hash lookup twice; a single get() with ?? is cheaper and reads better.

Key matching

Comparison is SameValueZero, identical to set(). has(NaN) is true after set(NaN, x) — note this differs from [NaN].includes-style equality only in that === would fail here. 0 and -0 are one key. Objects match by reference only.

If you only ever store booleans or ignore the values entirely, a Set expresses the intent better than a Map used purely through has().

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Guard a memoization cache whose function may return undefined
  • Detect duplicate keys while merging two sources
  • Check membership safely for keys that come from user input
  • Decide whether to initialize a bucket before appending to it

Frequently asked questions

What is the difference between Map.has() and Map.get()?

has() returns a boolean saying whether the key exists; get() returns the stored value. Only has() can tell a missing key apart from a key whose value is undefined.

Does the in operator work on a Map?

No. "key" in map tests object properties, not map entries, and would consult the prototype chain. Always use map.has(key).

Does Map.has() work with NaN as a key?

Yes. Map compares keys with SameValueZero, under which NaN equals itself, so has(NaN) finds an entry set with NaN.

More examples

A cache that tolerates undefined results

The second call to lookup("empty") prints "hit" even though the cached value is undefined. Testing with get() instead of has() would report a miss forever and recompute on every call.

Example · javascript
const cache = new Map();

function lookup(key) {
  if (cache.has(key)) {
    console.log("hit", key);
    return cache.get(key);
  }
  console.log("miss", key);
  const value = key === "empty" ? undefined : key.toUpperCase();
  cache.set(key, value);
  return value;
}

lookup("empty");
lookup("empty");
console.log(cache.size);

Finding duplicate keys while merging

has() is checked before set() overwrites, so "b" is recorded as a clash while "c" is a clean addition. After the loop "b" holds the new value but keeps its original position in the map.

Example · javascript
const base = new Map([["a", 1], ["b", 2]]);
const incoming = [["b", 20], ["c", 30]];
const clashes = [];

for (const [k, v] of incoming) {
  if (base.has(k)) clashes.push(k);
  base.set(k, v);
}

console.log(clashes);
console.log([...base]);

Discussion

  • Be the first to comment on this lesson.