WeakSet

WeakSet.prototype.has

Returns a boolean indicating whether the given object exists in the WeakSet

Syntax

JavaScript
weakSet.has(value)

Parameters

ParameterTypeDescription
valueobjectThe object to check for

Return Value

true if the object is in the WeakSet, false otherwise

Examples

Basic Usage
const ws = new WeakSet()
const a = {}
const b = {}
ws.add(a)
console.log(ws.has(a)) // true
console.log(ws.has(b)) // false
Practical Example
const seen = new WeakSet<object>()
function isCircular(obj: object): boolean {
  if (seen.has(obj)) return true
  seen.add(obj)
  for (const val of Object.values(obj)) {
    if (typeof val === 'object' && val && isCircular(val)) return true
  }
  return false
}
Advanced Usage
const initialized = new WeakSet<HTMLElement>()
function initWidget(el: HTMLElement) {
  if (initialized.has(el)) return
  initialized.add(el)
  el.textContent = 'Initialized'
}

Understanding WeakSet.prototype.has

The WeakSet.prototype.has method in JavaScript returns a boolean indicating whether the given object exists in the WeakSet. It belongs to the WeakSet object and is one of the most widely used methods for working with weakset values in modern JavaScript and TypeScript applications.

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

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

Browser support for WeakSet.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 WeakSet Methods

Other methods in the WeakSet object

Related Tools

More WeakSet Methods

Explore JavaScript Methods

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