GC handles it. When the function ends, the iterator can be collected.
Compatibility
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 ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
Android WebViewSupported · 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().