WeakMap

WeakMap.prototype.has

Returns a boolean indicating whether an element with the specified key exists in the WeakMap

Syntax

JavaScript
weakMap.has(key)

Parameters

ParameterTypeDescription
keyobjectThe key to test for presence

Return Value

true if the key exists in the WeakMap, false otherwise

Examples

Basic Usage
const wm = new WeakMap()
const obj = {}
wm.set(obj, 'value')
console.log(wm.has(obj)) // true
console.log(wm.has({})) // false
Practical Example
const visited = new WeakMap<object, boolean>()
function processOnce(obj: object) {
  if (visited.has(obj)) return
  visited.set(obj, true)
  console.log('Processing:', obj)
}
Advanced Usage
const wm = new WeakMap<object, string>()
const key = { id: 'test' }
wm.set(key, 'hello')
console.log(wm.has(key)) // true
// After key goes out of scope, entry is eligible for GC

Understanding WeakMap.prototype.has

The WeakMap.prototype.has method in JavaScript returns a boolean indicating whether an element with the specified key exists in the WeakMap. It belongs to the WeakMap object and is one of the most widely used methods for working with weakmap values in modern JavaScript and TypeScript applications.

The method signature is weakMap.has(key). It accepts 1 parameter: key. When called, it returns true if the key exists in the weakmap, false otherwise. Understanding when and how to use has() helps you write more expressive, readable code.

Common use cases for WeakMap.prototype.has include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like weakmap-get, weakmap-set, weakmap-delete, enabling you to chain operations together for complex data manipulation pipelines.

Browser support for WeakMap.prototype.has is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.

Browser Compatibility

Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.

Related Methods

More WeakMap Methods

Other methods in the WeakMap object

Related Tools

More WeakMap Methods

Explore JavaScript Methods

Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.