FinalizationRegistry.prototype.unregister
Unregisters a target object from the FinalizationRegistry using the unregister token provided during registration
Syntax
registry.unregister(unregisterToken)Parameters
| Parameter | Type | Description |
|---|---|---|
| unregisterToken | object | The token previously passed as the third argument to register() |
Return Value
A boolean: true if a registration was found and removed, false otherwise
Examples
const registry = new FinalizationRegistry(() => console.log('collected'))
const obj = {}
const token = {}
registry.register(obj, 'value', token)
registry.unregister(token)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)
}
}
}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)) // falseUnderstanding 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
FinalizationRegistry.prototype.registerRegisters a target object with the FinalizationRegistry so that the cleanup callback is called when the target is garbage collected
FinalizationRegistryCreates a new FinalizationRegistry that invokes a cleanup callback after a registered target object is garbage collected
WeakRef.prototype.derefReturns the target object of the WeakRef if it has not been garbage collected, otherwise returns undefined
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.