JavaScript WeakMap has() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
WeakMap

Definition

weakMap.has(key) returns true when key is stored in the WeakMap and false when it is not. It only asks: it does not read the value and it does not change anything. It never throws: a string, a number, or null simply gives false, because those can never be keys. Use it to guard work that should happen only once per object, or to tell a stored undefined from a missing key.

Remember
weakMap.has(key)  → true or false

key is stored       → true (even if its value is undefined)
key not stored      → false
key is not object   → false (no error)
after delete(key)   → false

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

Syntax

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

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

Built into JavaScript since ES2015. For the common "store it if it is missing, then read it" pattern, the newer getOrInsert() does both steps in one call.

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

// key: any value; only objects (and local symbols) can ever be found
// never throws

Parameters

ParameterDescription
keyRequired. The object (or local symbol) to look for. Any other value, including primitives, gives false.

Return Value

Return ValueDescription
trueThe key is stored in the WeakMap, even if its value is undefined.
falseThe key is not stored, was deleted, or can never be a key.

JavaScript Examples

Example 1 — Is This Key Stored?

Mark one page as visited, then ask about two pages and about the first page again after removing it.

JavaScript
const visited = new WeakMap();
const home = { url: "/home" };
const about = { url: "/about" };

visited.set(home, true);

console.log(visited.has(home));    // true
console.log(visited.has(about));   // false

visited.delete(home);
console.log(visited.has(home));    // false
Try It Yourself

How It Works

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

Example 2 — Do the Work Only Once per Object

Use has() as a guard so each task object is handled a single time.

JavaScript
const done = new WeakMap();

function handle(task) {
  if (done.has(task)) {
    return task.name + ": skipped";
  }
  done.set(task, true);
  return task.name + ": processed";
}

const backup = { name: "backup" };
const email = { name: "email" };
console.log(handle(backup));   // backup: processed
console.log(handle(backup));   // backup: skipped
console.log(handle(email));    // email: processed
Try It Yourself

How It Works

  1. Ask before acting. Each call starts with done.has(task). On the first call for backup the answer is false, so the work runs.
  2. Leave a mark. The function stores true for that task. The next call for the same object finds the mark and returns early.
  3. Marks belong to objects. email is a different object with no mark, so it is processed. Tasks that are no longer used can be cleaned up with their marks.

Example 3 — has() Does Not Throw, set() Does

Ask about values that can never be keys. You get false, while trying to store one raises an error.

JavaScript
const wm = new WeakMap();

console.log(wm.has(1));                  // false
console.log(wm.has("text"));             // false
console.log(wm.has(null));               // false
console.log(wm.has(Symbol.for("id")));   // false

try {
  wm.set("text", 1);
} catch (e) {
  console.log(e.name);                   // TypeError
}
Try It Yourself

How It Works

  1. Four odd keys, four calm answers. A number, a string, null, and a registered symbol are not allowed as keys, so has() says false for each.
  2. No error handling needed. You can pass any value from user input or JSON to has() without a try block.
  3. Only set() complains. Storing the string "text" breaks the rule, so set() throws a TypeError, which the catch block names.

Browser & Runtime Support

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

✓ Baseline · ES2015

WeakMap.prototype.has()

Works in every modern browser, plus Node.js, Deno, and Bun. Internet Explorer 11 has a basic WeakMap with set(), get(), has(), and delete(), but not the constructor argument.

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
WeakMap.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 WeakMap and returns true or false. It does not read the value and does not change the WeakMap.
No. Plain values can never be keys, so has() returns false. Only set() throws when you try to store such a key.
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.
WeakMap compares keys by identity. A new object with the same fields is a different key. Keep a reference to the exact object you stored.
No. A WeakMap has no size and cannot be looped over. You can only ask about keys you already hold, which is what makes weak keys safe for the garbage collector.

Next: set()

Learn how to store values, chain calls, and replace old ones.

set() tutorial →

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