JavaScript NodeIterator detach() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Method

Definition

NodeIterator.detach() is a no-op kept only for backward compatibility. Per MDN, it once invalidated the iterator; today it does nothing useful.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call iterator.detach. Start with this line.

JavaScript
nodeIterator.detach()
CallWhat it does
nodeIterator.detach()Primary signature.
iterator.detach(...)Call with the arguments your case needs.

Built into the browser. Call detach(...) on a NodeIterator — no import required.

Library declarations
JavaScript
nodeIterator.detach()

Parameters

This member has no parameters beyond those shown in the signature.

Return Value

Return ValueDescription
*undefined

JavaScript Examples

Example 1 — Returns undefined

detach() completes but gives no useful value.

JavaScript
const it = document.createNodeIterator(
  document.querySelector("#box"),
  NodeFilter.SHOW_ELEMENT
);
const result = it.detach();
console.log(result); // undefined
Try It Yourself

How It Works

  1. Create an iterator. Root is #box, show elements only.
  2. Call detach(). No arguments.
  3. Check the return. Always undefined.
  4. No cleanup signal. Modern engines do not need this call.

Example 2 — nextNode() After detach()

In modern browsers the iterator keeps working (no-op).

JavaScript
const it = document.createNodeIterator(
  document.querySelector("#box"),
  NodeFilter.SHOW_ELEMENT
);
it.detach();
const node = it.nextNode();
console.log(node ? node.nodeName : "null");
Try It Yourself

How It Works

  1. Detach first. Historically this invalidated the iterator.
  2. Call nextNode(). Modern engines still return a node.
  3. Print the name. Usually the root element (DIV).
  4. Do not rely on this. Remove detach() instead of testing it.

Example 3 — Throws Today?

MDN’s old example expected INVALID_STATE_ERR; modern browsers usually do not throw.

JavaScript
const nodeIterator = document.createNodeIterator(
  document.querySelector("#box"),
  NodeFilter.SHOW_ELEMENT,
  { acceptNode() { return NodeFilter.FILTER_ACCEPT; } }
);
nodeIterator.detach();
let threw = false;
try {
  nodeIterator.nextNode();
} catch (e) {
  threw = true;
}
console.log("threw INVALID_STATE_ERR? " + threw);
Try It Yourself

How It Works

  1. Build with a filter. Matches MDN’s historical sample shape.
  2. Call detach(). No-op today.
  3. Try nextNode(). Catch any exception.
  4. Expect false. Modern browsers do not invalidate the iterator.

Example 4 — typeof detach

The method still exists as a function for compatibility.

JavaScript
const it = document.createNodeIterator(
  document.querySelector("#box"),
  NodeFilter.SHOW_ELEMENT
);
console.log(typeof it.detach); // "function"
Try It Yourself

How It Works

  1. Create an iterator. Standard factory call.
  2. Inspect the method. typeof it.detach.
  3. Still a function. Kept so old scripts do not crash.
  4. Presence ≠ usefulness. Deprecated does not mean removed yet.

Example 5 — Walk Without detach()

Modern pattern: finish the loop and let the iterator go out of scope.

JavaScript
function collectTagNames(root) {
  const it = document.createNodeIterator(root, NodeFilter.SHOW_ELEMENT);
  const names = [];
  let node;
  while ((node = it.nextNode())) names.push(node.nodeName);
  return names; // no detach() — just return
}
console.log(JSON.stringify(
  collectTagNames(document.querySelector("#box"))
));
Try It Yourself

How It Works

  1. Create locally. it lives inside the function.
  2. Walk with nextNode. Collect element tag names.
  3. Return the result. No cleanup API call.
  4. GC handles it. When the function ends, the iterator can be collected.

Browser Support

NodeIterator.detach() is marked Deprecated on MDN. The method still exists in browsers as a no-op for compatibility. It is a browser DOM API — not a Node.js built-in.

✓ Deprecated · No-op

NodeIterator.detach()

Present across modern browsers for compatibility, but do not use it in new projects.

— Use in new code
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Android WebView Supported · Modern WebView
Full support
detach() Avoid

Bottom line: Do not call detach() in new code. Walk with nextNode/previousNode, then drop the iterator reference.

Frequently Asked Questions

In modern browsers it is a no-op kept for backward compatibility. MDN marks it Deprecated. It takes no arguments and returns undefined.
Yes. MDN marks NodeIterator.detach() as Deprecated. Do not call it in new code—simply stop using the iterator or let it go out of scope.
It detached the iterator from its node set, released resources, and set the iterator state to INVALID. After that, nextNode() and previousNode() threw INVALID_STATE_ERR.
In modern browsers detach() is a no-op, so nextNode() usually still works. Do not rely on that—remove detach() calls instead of depending on legacy cleanup.
Nothing specific. When you finish walking, stop calling the iterator. JavaScript garbage collection reclaims it when no references remain.
No. It is deprecated and unnecessary. Drop your reference to the NodeIterator rather than calling detach().

Next: nextNode()

Walk forward through the nodes your iterator accepts.

nextNode() →

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.

7 people found this page helpful