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)
Call
What 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
Parameter
Description
key
Required. The object (or local symbol) to look for. Any other value, including primitives, gives false.
Return Value
Return Value
Description
true
The key is stored in the WeakMap, even if its value is undefined.
false
The 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.
Ask before acting. Each call starts with done.has(task). On the first call for backup the answer is false, so the work runs.
Leave a mark. The function stores true for that task. The next call for the same object finds the mark and returns early.
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.
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.
No error handling needed. You can pass any value from user input or JSON to has() without a try block.
Only set() complains. Storing the string "text" breaks the rule, so set() throws a TypeError, which the catch block names.
Compatibility
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 Chrome36+ · Desktop & Mobile
Full support
Mozilla Firefox6+ · Desktop & Mobile
Full support
Apple Safari8+ · macOS & iOS
Full support
Microsoft Edge12+
Full support
Node.js0.12+ · every current release
Supported
DenoAll releases
Supported
Internet ExplorerIE 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.