W3cubDocs

/JavaScript

Symbol.dispose

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

The Symbol.dispose static data property represents the well-known symbol Symbol.dispose. The using declaration looks up this symbol on the variable initializer for the method to call when the variable goes out of scope.

Value

The well-known symbol Symbol.dispose.

Property attributes of Symbol.dispose
Writable no
Enumerable no
Configurable no

Description

An object is disposable if it has the [Symbol.dispose]() method. The method is expected to have the following semantics:

  • Invoking this method notifies the Disposable object that the caller does not intend to continue to use this object. This method should perform any necessary logic to explicit clean up the resource including, but not limited to, file system handles, streams, host objects, etc.
  • When an exception is thrown from this method, it typically means that the resource could not be explicitly freed.
  • If called more than once on the same object, the function should not throw an exception. However, this requirement is not enforced.

This method should not return a promise, as promises returned by [Symbol.dispose]() are not awaited by await using. To declare async disposables, use Symbol.asyncDispose.

Examples

>

User defined disposables

[Symbol.dispose] allows the creation of custom disposables. See the using reference for more information.

class Disposable {
  constructor() {
    this.disposed = false;
  }

  [Symbol.dispose]() {
    this.disposed = true;
  }

  get isDisposed() {
    return this.disposed;
  }
}

const resource = new Disposable();
{
  using resourceUsed = resource;
  console.log(resource.isDisposed); // false
}
console.log(resource.isDisposed); // true

Specifications

Browser compatibility

Desktop Mobile Server
Chrome Edge Firefox Opera Safari Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet WebView Android WebView on iOS Bun Deno Node.js
dispose 125 125 141 111 No 125 141 83 No 27.0 125 No 1.0.23 1.37 24.0.0
20.4.0–21.0.0Only available for fs and stream resources.
18.18.0–19.0.0Only available for fs and stream resources.

See also

© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/dispose