FinalizationRegistry

FinalizationRegistry.prototype.unregister

Unregisters a target object from the FinalizationRegistry using the unregister token provided during registration

Syntax

JavaScript
registry.unregister(unregisterToken)

Parameters

ParameterTypeDescription
unregisterTokenobjectThe token previously passed as the third argument to register()

Return Value

A boolean: true if a registration was found and removed, false otherwise

Examples

Basic Usage
const registry = new FinalizationRegistry(() => console.log('collected'))
const obj = {}
const token = {}
registry.register(obj, 'value', token)
registry.unregister(token)
Practical Example
class ManagedResource {
  private registry = new FinalizationRegistry<string>(key => console.log(`GC: ${key}`))
  private tokens = new Map<string, object>()
  track(key: string, obj: object) {
    const token = {}
    this.tokens.set(key, token)
    this.registry.register(obj, key, token)
  }
  release(key: string) {
    const token = this.tokens.get(key)
    if (token) {
      this.registry.unregister(token)
      this.tokens.delete(key)
    }
  }
}
Advanced Usage
const registry = new FinalizationRegistry((v: string) => console.log(v))
const token = {}
const target = {}
registry.register(target, 'hello', token)
console.log(registry.unregister(token)) // true
console.log(registry.unregister(token)) // false

Understanding FinalizationRegistry.prototype.unregister

The FinalizationRegistry.prototype.unregister method in JavaScript unregisters a target object from the FinalizationRegistry using the unregister token provided during registration. It belongs to the FinalizationRegistry object and is one of the most widely used methods for working with finalizationregistry values in modern JavaScript and TypeScript applications.

The method signature is registry.unregister(unregisterToken). It accepts 1 parameter: unregisterToken. When called, it returns a boolean: true if a registration was found and removed, false otherwise. Understanding when and how to use unregister() helps you write more expressive, readable code.

Common use cases for FinalizationRegistry.prototype.unregister include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like finalizationregistry-register, finalizationregistry-constructor, weakref-deref, enabling you to chain operations together for complex data manipulation pipelines.

Supported in Chrome 84+, Firefox 79+, Safari 14.1+, Edge 84+, Node.js 14.6+.

Browser Compatibility

Supported in Chrome 84+, Firefox 79+, Safari 14.1+, Edge 84+, Node.js 14.6+.

Related Methods

More FinalizationRegistry Methods

Other methods in the FinalizationRegistry object

Related Tools

More FinalizationRegistry Methods

Explore JavaScript Methods

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