Favorites

JavaScript using Declaration

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee

The using declaration is a new feature that is not yet supported in every browser. Check browser support before using it in production.

using
JavaScript DeclarationsThis declaration is supported in some modern browsers, but not all.
Updated 28th November 2026

The using declaration creates a block-scoped constant for a resource that is cleaned up automatically, by calling its Symbol.dispose method, when the block ends.

Description

The using declaration declares a block-scoped, read-only variable for a resource that needs cleaning up, such as a file handle, lock or subscription. When the enclosing block exits, whether normally or because of an error, JavaScript automatically calls the resource's [Symbol.dispose]() method. await using does the same with [Symbol.asyncDispose](), awaiting it.

Resources are disposed in the reverse order they were declared. It replaces many try...finally clean-up blocks. This feature, called explicit resource management, is new, so check support before relying on it.

A resource for using is any object with a [Symbol.dispose]() method (or null or undefined, which are simply skipped). If disposing throws an error while another error is already being thrown, both are kept in a SuppressedError. The DisposableStack and AsyncDisposableStack objects let you collect several resources, or clean-up callbacks, and dispose of them together, which is useful when the number of resources isn't known in advance.

Syntax

using name = resource;
await using name = asyncResource;

Example

function openLog(name) {
  console.log('open', name);
  return { name, [Symbol.dispose]() { console.log('closed', name); } };
}

{
  using a = openLog('first');
  using b = openLog('second');
  console.log('working...');
}
console.log('after the block');

Browser Support

The following information will show you the current browser support for the using declaration. Hover over a browser icon to see the version that first introduced support for this declaration.

This declaration is supported in some modern browsers, but not all.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee