JavaScript Iterator [Symbol.dispose]() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Instance method

Definition

iterator[Symbol.dispose]() implements the disposable protocol for iterators. On Iterator.prototype, it simply calls the iterator's return() method when that method exists, so cleanup can run early.

Language using declarations call this for you at the end of a block. In everyday try-it code you can call iterator[Symbol.dispose]() yourself, or call return() directly on a generator — both close the iterator when return is present.

Remember
iterator[Symbol.dispose]()

calls return() if it exists
used by: using / await using

generators: finally { ... } runs on close
you can also call iterator.return()

returns undefined

Syntax

Call it with square brackets on the symbol. Most cleanup examples work by showing that return() runs.

JavaScript
function* numbers() {
  try {
    yield 1;
    yield 2;
  } finally {
    console.log("cleaned up");
  }
}

const it = numbers();
it.next();
it[Symbol.dispose]();   // cleaned up
CallWhat it does
iterator[Symbol.dispose]()Close the iterator by calling return() when present.
iterator.return()The classic early-close method; dispose delegates to this.
using it = makeIterator()Language form that calls dispose automatically (where supported).
typeof Symbol.disposeFeature-detect the dispose symbol before use.

A newer method on Iterator.prototype for Explicit Resource Management. It lives under the symbol key Symbol.dispose. Feature-detect before you rely on it. Siblings: Iterator.concat(), Iterator.from(), Iterator.zip(), Iterator.zipKeyed(), chunks(), drop(), every(), filter(), find(), flatMap(), forEach(), includes(), join(), map(), reduce(), some(), take(), toArray(), windows(), [Symbol.iterator]().

Library declarations
JavaScript
// Explicit Resource Management (newer)
Iterator.prototype[Symbol.dispose]()

// (no parameters)
// effect : calls this.return() if it is a function
// returns: undefined

Parameters

ParameterDescription
(none)This method takes no parameters. Call it as iterator[Symbol.dispose]().

Return Value

Return ValueDescription
undefinedNothing useful is returned. The point is the side effect: closing the iterator so cleanup can run.

JavaScript Examples

Example 1 — Call Dispose on a Generator

Start a generator, read one value, then call [Symbol.dispose]() so the finally block runs.

JavaScript
function* numbers() {
  try {
    yield 1;
    yield 2;
    yield 3;
  } finally {
    console.log("cleaned up");
  }
}

const it = numbers();
console.log("First: " + it.next().value);
it[Symbol.dispose]();
console.log("After dispose done: " + it.next().done);
Try It Yourself

The Try It page first checks that iterator[Symbol.dispose]() exists, because Iterator helpers are still new. In an older browser it prints a short explanation and the output above. In a current browser it runs the code for real.

How It Works

  1. Read one value. next() yields 1 and pauses inside the generator.
  2. Dispose early. [Symbol.dispose]() calls return(), which runs the finally block and prints cleaned up.
  3. Iterator is finished. Another next() reports done: true.

Example 2 — Dispose Calls return() on a Custom Iterator

Build a tiny iterator with a return() method and show that dispose closes it.

JavaScript
const it = {
  closed: false,
  next() {
    return { value: "tick", done: false };
  },
  return() {
    this.closed = true;
    console.log("return() ran");
    return { done: true };
  }
};

Object.setPrototypeOf(it, Iterator.prototype);

console.log("Value: " + it.next().value);
it[Symbol.dispose]();
console.log("Closed: " + it.closed);
Try It Yourself

The Try It page first checks that iterator[Symbol.dispose]() exists, because Iterator helpers are still new. In an older browser it prints a short explanation and the output above. In a current browser it runs the code for real.

How It Works

  1. Custom iterator. It has next() and return(), plus a closed flag.
  2. Use Iterator.prototype. Setting the prototype gives it the shared [Symbol.dispose] method.
  3. Dispose closes it. Dispose calls return(), which logs and sets closed to true.

Example 3 — Feature-Detect Symbol.dispose

Check that the symbol and the prototype method exist before you call them.

JavaScript
const hasSymbol = typeof Symbol.dispose !== "undefined";
const hasMethod = hasSymbol && typeof Iterator.prototype[Symbol.dispose] === "function";

console.log("Symbol.dispose: " + hasSymbol);
console.log("Iterator dispose method: " + hasMethod);

if (hasMethod) {
  function* one() {
    try {
      yield "ok";
    } finally {
      console.log("closed");
    }
  }
  const it = one();
  it.next();
  it[Symbol.dispose]();
}
Try It Yourself

The Try It page first checks that iterator[Symbol.dispose]() exists, because Iterator helpers are still new. In an older browser it prints a short explanation and the output above. In a current browser it runs the code for real.

How It Works

  1. Check the symbol. Older engines may not define Symbol.dispose at all.
  2. Check the method. Confirm Iterator.prototype[Symbol.dispose] is a function.
  3. Only then dispose. When both exist, dispose closes the generator and prints closed.

Browser & Runtime Support

Iterator.prototype[Symbol.dispose]() comes with the Explicit Resource Management proposal (using / await using). Support is still rolling out across engines.

✓ Newer · Explicit Resource Management

iterator[Symbol.dispose]()

Supported in Chrome 134+, Edge 134+, Firefox 141+, Safari 26+, Opera 119+ and Node.js 24+. Internet Explorer never got it.

Recent Standard since
Google Chrome 134+ · Desktop & Mobile
Full support
Microsoft Edge 134+ · Chromium based
Full support
Mozilla Firefox 141+ · Desktop & Mobile
Full support
Apple Safari 26+ · macOS & iOS
Full support
Opera 119+ · Chromium based
Full support
Samsung Internet Recent · Android
Supported
Android WebView 134+ · Chromium based
Supported
Node.js 24+ · Server runtime
Supported
Deno Recent versions · JavaScript runtime
Supported
Bun Recent versions · JavaScript runtime
Supported
Internet Explorer No native support
Not supported
iterator[Symbol.dispose]() Recent engines

Bottom line: Feature-detect Symbol.dispose and the using syntax. Calling iterator.return() by hand is the classic way to close an iterator early.

Frequently Asked Questions

It closes the iterator for cleanup. On Iterator.prototype it calls return() when that method exists, then returns undefined.
No for learning. using calls dispose for you where the syntax is supported. You can also call iterator[Symbol.dispose]() or iterator.return() by hand.
Use square brackets with the symbol: iterator[Symbol.dispose](). Do not write iterator.Symbol.dispose() or quote the name as a normal string key.
Then dispose has nothing useful to call and cleanup is a no-op. Generators always provide return(), which runs finally blocks.
Check typeof Symbol.dispose !== "undefined" and typeof Iterator.prototype[Symbol.dispose] === "function" before calling it.

Next: [Symbol.iterator]()

Learn why iterators return themselves from [Symbol.iterator]() so for...of and spread work on them.

[Symbol.iterator]() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful