using
In JavaScript, using is a contextual keyword that declares a block-scoped constant whose value is disposed of automatically when its scope exits. The value must be an object with a [Symbol.dispose]() method, or null or undefined, and any other value throws a TypeError. Like const, a using binding can’t be reassigned, and it has the same temporal dead zone as let.
When the scope exits, normally or through an error, the disposers run in reverse order of declaration. A disposer that throws while another error is propagating produces a SuppressedError. The declaration works in blocks, function bodies, for and for...of heads, and at the top level of a module, but not at the top level of a script. Elsewhere, using is an ordinary identifier.
The asynchronous counterpart is await using, and DisposableStack manages disposal by hand.
Note: using reached Stage 4 in May 2026 and is expected in ES2027. It ships in Chrome and Edge 134, Firefox 141, and Node.js 24, but as of 2026 only in Safari Technology Preview, so it has Limited availability in Baseline terms.
JavaScript using Keyword Syntax
A using declaration can stand on its own or start a for...of loop:
using <name> = <value>[, <name2> = <value2>, ...];
for (using <name> of <iterable>) {
<statements>
}
Every binding needs an initializer, and the name can’t be a destructuring pattern.
JavaScript using Keyword Examples
Two connections declared with using close automatically when the block ends:
> function openConnection(name) {
… console.log(`Opening ${name}`);
… return {
… [Symbol.dispose]() {
… console.log(`Closing ${name}`);
… },
… };
… }
undefined
> {
… using primary = openConnection("primary");
… using replica = openConnection("replica");
… console.log("Running queries");
… }
Opening primary
Opening replica
Running queries
Closing replica
Closing primary
undefined
Both connections close at the closing brace, and the one declared last closes first.
Disposal also runs when the block throws, which is the cleanup a finally block would otherwise do:
> {
… using connection = { [Symbol.dispose]: () => console.log("Closed") };
… throw new Error("Query failed");
… }
Closed
Uncaught Error: Query failed
JavaScript using Keyword Use Cases
- Closing file handles, sockets, and database connections at the end of a block
- Releasing locks and other guards when a critical section ends
- Disposing of each resource in a
for...ofloop as its iteration finishes
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Nov. 4, 2026