JavaScript NodeIterator whatToShow Property

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance property

What You’ll Learn

The whatToShow property of a NodeIterator returns a read-only bitmask of node types the iterator will consider. Learn how it mirrors the second argument to createNodeIterator, how NodeFilter.SHOW_ELEMENT and SHOW_TEXT change what you see, and how it works with filter—with five examples and try-it labs.

01

Kind

Instance property

02

Returns

unsigned long

03

Bitmask

NodeFilter flags

04

Set at

createNodeIterator

05

vs filter

type vs custom

06

Status

Baseline widely

Introduction

When you call document.createNodeIterator(root, whatToShow, filter), the second argument tells the browser which node types to include in the walk—elements, text nodes, comments, and more. The iterator stores that value on its read-only whatToShow property.

Think of whatToShow as a coarse pre-filter. It runs before your custom filter acceptNode() logic. If a node type is not in the bitmask, the iterator skips it entirely.

💡
Beginner tip

Start with NodeFilter.SHOW_ELEMENT when you only want tags like <p> and <span>. Use NodeFilter.SHOW_TEXT when you care about text content inside elements.

Understanding the whatToShow Property

A read-only instance property on NodeIterator that returns a non-negative integer representing a bitmask of NodeFilter constants.

  • Returns — an unsigned long (bitmask).
  • Read-only — set at creation, never changes.
  • Source — second argument to createNodeIterator.
  • Combine flags — use bitwise OR, e.g. SHOW_ELEMENT | SHOW_TEXT.
  • DefaultNodeFilter.SHOW_ALL when omitted.
  • Baseline Widely available on MDN (since July 2015).

Common NodeFilter constants

ConstantTypical use
SHOW_ELEMENTElement nodes (tags)
SHOW_TEXTText nodes (#text)
SHOW_COMMENTHTML comments
SHOW_DOCUMENTDocument node
SHOW_ALLAll node types (default)

📝 Syntax

Read the property on a NodeIterator:

JavaScript
nodeIterator.whatToShow

Return value

A non-negative integer bitmask. See document.createNodeIterator() for the full list of flags.

Typical pattern (MDN idea)

JavaScript
const nodeIterator = document.createNodeIterator(
  document.body,
  NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_COMMENT | NodeFilter.SHOW_TEXT,
  { acceptNode: () => NodeFilter.FILTER_ACCEPT },
);

if (
  nodeIterator.whatToShow & NodeFilter.SHOW_ALL ||
  nodeIterator.whatToShow & NodeFilter.SHOW_COMMENT
) {
  // iterator may visit comment nodes
}

⚡ Quick Reference

GoalCode / note
Read maskiterator.whatToShow
Elements onlyNodeFilter.SHOW_ELEMENT
Text onlyNodeFilter.SHOW_TEXT
Combine typesSHOW_ELEMENT | SHOW_TEXT
Test a flagiterator.whatToShow & NodeFilter.SHOW_COMMENT
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about NodeIterator.whatToShow.

Returns
bitmask

unsigned long

Fixed?
yes

Set at create

Writable?
no

Read-only

Baseline
widely

Since Jul 2015

Examples Gallery

Examples follow MDN NodeIterator.whatToShow. Labs show how the bitmask controls which node types appear during a walk.

📚 Getting Started

Read whatToShow after createNodeIterator.

Example 1 — Read whatToShow with SHOW_ELEMENT

Create an iterator for elements only and confirm the stored bitmask.

JavaScript
const it = document.createNodeIterator(
  document.getElementById("box"),
  NodeFilter.SHOW_ELEMENT,
  { acceptNode: () => NodeFilter.FILTER_ACCEPT },
);

console.log(it.whatToShow === NodeFilter.SHOW_ELEMENT); // true
Try It Yourself

How It Works

The second argument to createNodeIterator is copied to the read-only whatToShow property.

Example 2 — SHOW_ELEMENT vs SHOW_TEXT

Same markup, different masks—elements vs text nodes in the walk.

JavaScript
const box = document.getElementById("box");

const itEl = document.createNodeIterator(box, NodeFilter.SHOW_ELEMENT);
const itText = document.createNodeIterator(box, NodeFilter.SHOW_TEXT);

const elNames = [];
let n;
while ((n = itEl.nextNode())) elNames.push(n.nodeName);

const textValues = [];
while ((n = itText.nextNode())) textValues.push(n.nodeValue.trim());

console.log(elNames);    // ["DIV", "SPAN", "SPAN"]
console.log(textValues); // ["Hello", "World"]
Try It Yourself

How It Works

SHOW_ELEMENT visits tags; SHOW_TEXT visits the text inside them. Whitespace-only text nodes may also appear depending on markup.

📈 Bitmasks & Defaults

Defaults, combinations, and testing flags.

Example 3 — Default SHOW_ALL Bitmask

When you omit the second argument, whatToShow is SHOW_ALL.

JavaScript
const it = document.createNodeIterator(document.getElementById("box"));

console.log(it.whatToShow === NodeFilter.SHOW_ALL); // true
Try It Yourself

How It Works

SHOW_ALL is the widest mask—the iterator can consider every node type it supports.

Example 4 — Combine Flags with Bitwise OR (MDN Idea)

Merge element, comment, and text types into one mask.

JavaScript
const mask =
  NodeFilter.SHOW_ELEMENT |
  NodeFilter.SHOW_COMMENT |
  NodeFilter.SHOW_TEXT;

const nodeIterator = document.createNodeIterator(
  document.body,
  mask,
  { acceptNode: () => NodeFilter.FILTER_ACCEPT },
);

console.log(nodeIterator.whatToShow === mask); // true
Try It Yourself

How It Works

MDN’s sample combines three constants. The stored whatToShow equals that combined integer.

Example 5 — Test the Bitmask with &

Check whether comments are included before walking (MDN pattern).

JavaScript
const nodeIterator = document.createNodeIterator(
  document.body,
  NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_COMMENT | NodeFilter.SHOW_TEXT,
  { acceptNode: () => NodeFilter.FILTER_ACCEPT },
);

const showsComments =
  (nodeIterator.whatToShow & NodeFilter.SHOW_ALL) ||
  (nodeIterator.whatToShow & NodeFilter.SHOW_COMMENT);

console.log(showsComments); // true
Try It Yourself

How It Works

Bitwise & tests whether a flag is set in the mask—useful when iterators are created elsewhere and you inspect them later.

🚀 Common Use Cases

  • Walk only element nodes and skip text noise with SHOW_ELEMENT.
  • Extract text content with SHOW_TEXT inside a subtree.
  • Include HTML comments in a walk with SHOW_COMMENT.
  • Inspect an iterator’s mask when debugging third-party code.
  • Pair a coarse whatToShow mask with a fine filter.

🔧 How It Works

1

Pass whatToShow mask

createNodeIterator(root, whatToShow, filter) stores the bitmask on the iterator.

Create
2

Pre-screen node type

During nextNode(), nodes whose type is not in the mask are skipped automatically.

Mask
3

Run filter.acceptNode

If the type passes, your custom filter can still accept or reject the node.

Filter
4

Read iterator.whatToShow

The read-only property always reflects the mask you chose at creation.

📝 Notes

  • MDN: Baseline Widely available (since July 2015) — no Deprecated / Experimental / Non-standard banner.
  • whatToShow is read-only and fixed for the iterator’s lifetime.
  • Use bitwise OR to combine multiple NodeFilter constants.
  • Do not confuse with filter, which adds custom accept/reject logic.
  • Related learning: root, filter, childNodes.

Universal Browser Support

NodeIterator.whatToShow is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

NodeIterator.whatToShow

Read-only bitmask of node types the iterator considers—set as the second argument to createNodeIterator.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Supported on NodeIterator (legacy DOM)
Legacy OK
NodeIterator.whatToShow Excellent

Bottom line: iterator.whatToShow mirrors the NodeFilter mask you pass to createNodeIterator—it never changes during the walk.

Conclusion

NodeIterator.whatToShow is the read-only bitmask that controls which node types an iterator considers. Set it as the second argument to createNodeIterator, combine flags with bitwise OR, and pair it with filter for finer control.

Continue with detach(), filter, childNodes, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use SHOW_ELEMENT when you only need tags
  • Combine flags with | when you need multiple types
  • Read iterator.whatToShow to debug iterator setup
  • Layer a custom filter after choosing a mask
  • Prefer the smallest useful mask for clearer walks

❌ Don’t

  • Try to assign a new value to iterator.whatToShow
  • Confuse whatToShow with filter.acceptNode
  • Always use SHOW_ALL when a narrower mask works
  • Forget that text nodes are separate from element nodes
  • Assume changing the mask later without creating a new iterator

Key Takeaways

Knowledge Unlocked

Five things to remember about NodeIterator.whatToShow

The node-type bitmask for DOM walks.

5
Core concepts
⚙️02

Fixed

at create

Rule
🔒03

SHOW_*

NodeFilter

Flags
📄04

vs filter

type mask

Compare
🎯05

Baseline

since Jul 2015

Status

❓ Frequently Asked Questions

A read-only non-negative integer (unsigned long)—a bitmask of NodeFilter constants that tells the iterator which node types to consider during traversal.
No. MDN marks NodeIterator.whatToShow as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
No. whatToShow is read-only. Create a new NodeIterator with a different second argument if you need another node-type mask.
whatToShow is a coarse bitmask of node kinds (elements, text, comments, etc.). filter is a custom acceptNode function for finer screening—e.g. only certain elements.
When you omit the second argument to createNodeIterator, the default is NodeFilter.SHOW_ALL (0xFFFFFFFF), which includes every node type the iterator can visit.
Use bitwise OR with NodeFilter constants, e.g. NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT. The stored whatToShow property reflects that combined mask.
Did you know?

MDN’s example combines SHOW_ELEMENT | SHOW_COMMENT | SHOW_TEXT, then tests the mask with bitwise &. That pattern lets you inspect an iterator and know whether comments are in scope before you walk.

Explore detach()

Learn why this deprecated cleanup method is a no-op today.

detach() method →

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.

5 people found this page helpful