Example 1 — root Is document.body
MDN idea: pass document.body and read the same node back.
const it = document.createNodeIterator(
document.body,
NodeFilter.SHOW_ELEMENT,
{ acceptNode: () => NodeFilter.FILTER_ACCEPT }
);
console.log(it.root === document.body); // true
console.log(it.root.nodeName); // "BODY" How It Works
- Pass the root.
document.bodyis the first argument. - Create the iterator. The browser stores that node as the walk boundary.
- Read it back.
it.root === document.bodyistrue. - Inspect the node.
nodeNameis"BODY".

