JavaScript Map has() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Map

Definition

map.has(key) returns true when key is stored in the Map and false when it is not. It only asks: it does not read the value and it does not change anything. Keys are matched with SameValueZero, so a NaN key is found. Object keys are matched by identity. Use it to tell a stored undefined from a missing key.

Remember
map.has(key)  → true or false

key is stored       → true (even if its value is undefined)
key not stored      → false
NaN key             → true when NaN was stored
different object    → false (same shape is not enough)

has() answers "is it there?", get() answers "what is it?"

Syntax

Call has on a Map with the key you want to look for.

JavaScript
map.has(key)
CallWhat it does
map.has(key)Return true if key is stored, otherwise false.
if (!map.has(key)) { ... }Run some setup only when the key has not been stored yet.

Built into JavaScript since ES2015. For reading the value after you know the key exists, see get().

Library declarations
JavaScript
map.has(key)   // returns a boolean

// key: any value; SameValueZero matching
// never throws

Parameters

ParameterDescription
keyRequired. Any value to look for. Objects must be the same instance that was passed to set().

Return Value

Return ValueDescription
trueThe key is stored in the Map, even if its value is undefined.
falseThe key is not stored, or was deleted.

JavaScript Examples

Example 1 — Is This Key Stored?

Mark one product as in stock, then ask about two products and about the first one again after removing it.

JavaScript
const stock = new Map();
stock.set("mug", 12);

console.log(stock.has("mug"));    // true
console.log(stock.has("cup"));    // false

stock.delete("mug");
console.log(stock.has("mug"));    // false
Try It Yourself

How It Works

  1. A stored key answers true. stock.set("mug", 12) adds the entry, so has("mug") finds it.
  2. An unknown key answers false. "cup" was never stored. has() only reports this; it does not add the key.
  3. Removing changes the answer. After delete("mug") the entry is gone, so the same has("mug") call now says false.

Example 2 — has() Finds a NaN Key

Store a tag under NaN. Asking with another NaN still returns true.

JavaScript
const tags = new Map();
tags.set(NaN, "bad input");

console.log(tags.has(NaN));           // true
console.log(tags.has(Number("x")));   // true
console.log(tags.has(0));             // false
Try It Yourself

How It Works

  1. NaN can be stored. tags.set(NaN, "bad input") adds one entry under NaN.
  2. SameValueZero matches. has(NaN) and has(Number("x")) both find that entry, even though NaN === NaN is false in normal JavaScript.
  3. Other keys stay separate. has(0) is false. Zero is not NaN.

Example 3 — Objects Match by Identity

Store a count for one cart object. A new object with the same fields is a different key.

JavaScript
const counts = new Map();
const cart = { id: 7 };

counts.set(cart, 3);

console.log(counts.has(cart));          // true
console.log(counts.has({ id: 7 }));     // false
console.log(counts.size);               // 1
Try It Yourself

How It Works

  1. The same object is found. has(cart) uses the exact object that was passed to set(), so the answer is true.
  2. A look-alike is not enough. A new { id: 7 } has the same fields but is a different object, so has() returns false.
  3. Only one entry exists. Size stays 1. Keep a reference to the object you stored if you need to ask about it later.

Browser & Runtime Support

Map.prototype.has() is part of ES2015 (ES6), together with WeakMap, Set, and WeakSet.

✓ Baseline · ES2015

Map.prototype.has()

Works in every modern browser, plus Node.js, Deno, and Bun. Internet Explorer 11 has a basic Map with set(), get(), has(), delete(), clear(), and size.

98% Modern browser support
Google Chrome 36+ · Desktop & Mobile
Full support
Mozilla Firefox 6+ · Desktop & Mobile
Full support
Apple Safari 8+ · macOS & iOS
Full support
Microsoft Edge 12+
Full support
Node.js 0.12+ · every current release
Supported
Deno All releases
Supported
Internet Explorer IE 11 · no constructor argument
Basic
Map.prototype.has() Excellent

Bottom line: Safe to use in any modern browser and runtime. has() behaves the same everywhere and never throws.

Frequently Asked Questions

It checks whether a key is stored in the Map and returns true or false. It does not read the value and does not change the Map.
has() answers whether the key is there. get() returns the stored value. If the stored value can be undefined, has() is the only way to tell that apart from a missing key.
Yes. Map uses SameValueZero, so has(NaN) is true when NaN was stored, even though NaN === NaN is false.
Map compares object keys by identity. A new object with the same fields is a different key. Keep a reference to the exact object you stored.
No. It always returns true or false. Missing keys and odd key types simply answer false.

Next: delete()

Remove one entry and see how has() and size change together.

delete() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful