document.createTreeWalker() is an instance method that returns a TreeWalker for walking a DOM subtree (see MDN Document: createTreeWalker()). Learn root, whatToShow, custom filters, nextNode() loops, tree moves like firstChild, how it compares to NodeIterator, and five try-it labs.
01
Kind
Instance method
02
Returns
TreeWalker
03
Start
root = currentNode
04
Filter
whatToShow
05
Walk with
nextNode()
06
Status
Baseline
Fundamentals
Introduction
A TreeWalker is a cursor for a DOM branch. You pick a root, optionally filter which node types matter, then move with nextNode() — or step like a real tree using firstChild(), nextSibling(), and friends.
document.createTreeWalker(root, whatToShow, filter) builds that walker. MDN’s first example walks every text node under #root and uppercases node.data.
💡
Think: filtered tree cursor
1) Pick a root (becomes currentNode) 2) Limit types with whatToShow (optional) 3) Accept / reject / skip with a filter (optional) 4) Loop nextNode() or use tree moves
For simple element lists, querySelectorAll is often enough. Use TreeWalker when you need text/comment traversal or parent/sibling navigation.
root — a Node that is the root of the TreeWalker and the initial currentNode (MDN).
whatToShow(optional) — unsigned long bitmask from NodeFilter constants. Defaults to 0xFFFFFFFF ((SHOW_ALL) (MDN).
filter(optional) — callback or { acceptNode() } returning FILTER_ACCEPT, FILTER_REJECT, or FILTER_SKIP (MDN).
Return value
A new TreeWalker object (MDN).
Filter return values (MDN)
FILTER_ACCEPT — include this node.
FILTER_REJECT — exclude this node and its subtree.
FILTER_SKIP — exclude this node only (descendants may still be visited).
Useful whatToShow flags
Constant
Shows
NodeFilter.SHOW_ALL
All nodes (default)
NodeFilter.SHOW_ELEMENT
Element nodes
NodeFilter.SHOW_TEXT
Text nodes
NodeFilter.SHOW_COMMENT
Comment nodes
NodeFilter.SHOW_DOCUMENT
Document nodes
NodeFilter.SHOW_DOCUMENT_FRAGMENT
DocumentFragment nodes
Combine flags with |, for example NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT. MDN: SHOW_ATTRIBUTE is only useful when the root is an Attr node — prefer Element.attributes for attributes. Some older constants (SHOW_ENTITY, …) are legacy and no longer effective.
Document.createTreeWalker() is Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Document.createTreeWalker()
Create TreeWalker cursors for filtered DOM traversal across all major browsers.
BaselineWidely available
Google ChromeSupported
Yes
Mozilla FirefoxSupported
Yes
Apple SafariSupported
Yes
Microsoft EdgeSupported
Yes
OperaSupported
Yes
Internet ExplorerSupported (legacy)
Yes
createTreeWalker()Wide
Bottom line: Use createTreeWalker when you need filtered subtree walks or parent/sibling navigation. Prefer querySelectorAll for simple element lists.
Wrap Up
Conclusion
document.createTreeWalker(root, whatToShow, filter) returns a TreeWalker cursor for a DOM branch. Set whatToShow, optionally filter with ACCEPT / REJECT / SKIP, then walk with nextNode() or tree moves — just like MDN’s text-transform examples.
Choose a tight root so you do not walk the whole document
Use SHOW_TEXT / SHOW_ELEMENT instead of filtering everything in JS
Use FILTER_REJECT when a whole branch should be ignored (MDN)
Prefer TreeWalker when you need sibling/parent navigation
Prefer querySelectorAll for simple element queries
❌ Don’t
Confuse FILTER_REJECT with FILTER_SKIP (subtree vs node)
Rely on SHOW_ATTRIBUTE for normal element trees (MDN)
Forget that currentNode starts at the root
Mutate the tree carelessly while walking (can surprise the cursor)
Use TreeWalker when a CSS selector already answers the question
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about createTreeWalker()
Build a filtered DOM cursor with optional tree navigation.
5
Core concepts
📝01
Returns
TreeWalker
MDN
📄02
Root
currentNode
start
🔎03
Filter
whatToShow
bitmask
⚖️04
Reject
skips subtree
MDN
🛡05
Status
Baseline
2015
❓ Frequently Asked Questions
MDN: Document.createTreeWalker() returns a newly created TreeWalker object rooted at a given Node, optionally filtered by whatToShow and a filter callback.
No. MDN marks Document.createTreeWalker() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
A new TreeWalker object (MDN). TreeWalker.currentNode starts as the root you passed in.
Both walk a filtered subtree. TreeWalker also supports tree-shaped moves such as parentNode, firstChild, lastChild, previousSibling, and nextSibling. Prefer createNodeIterator for a simple forward nextNode loop.
MDN: an optional bitmask from NodeFilter constants (for example SHOW_TEXT or SHOW_ELEMENT). It defaults to SHOW_ALL (0xFFFFFFFF).
MDN: FILTER_ACCEPT includes the node; FILTER_REJECT excludes that node and its whole subtree; FILTER_SKIP excludes only that node (descendants may still be visited).
Did you know?
On a NodeIterator, MDN treats FILTER_REJECT and FILTER_SKIP as equivalent. On a TreeWalker, they are different: reject cuts off the whole branch. That difference is one of the best reasons to learn both APIs.