isWeakMapKey
Checks whether a value can be used as a key in a WeakMap (or a WeakSet member / WeakRef
target) — an object, a function (despite typeof reporting 'function', functions are
objects), or an unregistered symbol (a plain Symbol(...) or a well-known symbol like
Symbol.iterator).
Symbols registered via Symbol.for(...) are excluded: since they live forever in the global
symbol registry, the language disallows them as weak references (ES2023’s “Symbols as
WeakMap keys”). This is the part of the check easy to get wrong — typeof value === 'symbol'
alone accepts registered symbols too, which actually throw at WeakMap.prototype.set.
Available since v3.0.8
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
value | unknown | The value to check |
Returns
Section titled “Returns”value is symbol | object — true if value is a valid WeakMap/WeakSet key or WeakRef target
Examples
Section titled “Examples”Objects and functions are valid WeakMap keys
Section titled “Objects and functions are valid WeakMap keys”Functions pass despite typeof reporting “function” — they are objects.
Unregistered symbols pass, registered ones do not
Section titled “Unregistered symbols pass, registered ones do not”Symbol.for(…) symbols live forever in the global registry, so the language disallows them as weak references.
