JavaScript NodeIterator whatToShow Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Property

Definition

NodeIterator.whatToShow is a read-only unsigned integer bitmask that says which node types the iterator returns. You set it as the second argument to createNodeIterator() using NodeFilter constants (see MDN).

Read the notes below before you rely on this property. Prefer feature detection when support is limited.

Syntax

Use iterator.whatToShow. Start with this line.

JavaScript
iterator.whatToShow
CallWhat it does
iterator.whatToShowPrimary signature.

Built into the browser. Read iterator.whatToShow on a NodeIterator — no import required.

Library declarations
JavaScript
iterator.whatToShow

Parameters

ParameterDescription
whatToShow at createOptional. Defaults to SHOW_ALL (0xFFFFFFFF)
filterOptional. Finer screening after the bitmask

Return Value

Return ValueDescription
*Non-negative integer (bitmask)

JavaScript Examples

Example 1 — Read SHOW_ELEMENT

Pass a single flag and confirm whatToShow stores it.

JavaScript
const root = document.createElement("div");
root.innerHTML = "<span>A</span>";

const it = document.createNodeIterator(
  root,
  NodeFilter.SHOW_ELEMENT,
  { acceptNode: () => NodeFilter.FILTER_ACCEPT }
);

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

How It Works

  1. Pass the mask. Second argument is SHOW_ELEMENT.
  2. Create the iterator. The browser stores that bitmask.
  3. Read it back. it.whatToShow === NodeFilter.SHOW_ELEMENT.
  4. See the number. SHOW_ELEMENT is 1 (0x1).

Example 2 — SHOW_ELEMENT vs SHOW_TEXT

Same markup, two masks—different node lists.

JavaScript
const root = document.createElement("div");
root.innerHTML = "<span>Hello</span><span>World</span>";

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

const elements = [];
const texts = [];
let n;
while ((n = itEl.nextNode())) elements.push(n.nodeName);
while ((n = itText.nextNode())) {
  const t = n.nodeValue.trim();
  if (t) texts.push(t);
}

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

How It Works

  1. Build one tree. A root with two spans of text.
  2. Walk elements. SHOW_ELEMENT yields DIV and the SPANs.
  3. Walk text. SHOW_TEXT yields the text nodes instead.
  4. Trim whitespace. Skip empty text nodes from formatting.

Example 3 — Combine Flags with |

MDN idea: OR several constants, then test a flag with &.

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

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

console.log(it.whatToShow === mask); // true
console.log((it.whatToShow & NodeFilter.SHOW_COMMENT) !== 0); // true
console.log((it.whatToShow & NodeFilter.SHOW_DOCUMENT) !== 0); // false
Try It Yourself

How It Works

  1. OR the flags. One mask can include several node kinds.
  2. Store on create. it.whatToShow equals that combined mask.
  3. Test with AND. & SHOW_COMMENT is non-zero when comments are included.
  4. Negative check. SHOW_DOCUMENT was not OR’d in, so the test is false.

Browser & Runtime Support

NodeIterator.whatToShow is marked Baseline Widely available on MDN (since July 2015). It is not Deprecated, Experimental, or Non-standard.

✓ Baseline · Widely available

NodeIterator.whatToShow

Read-only NodeFilter bitmask. Supported across modern Chrome, Firefox, Safari, and Edge. Available since July 2015 per MDN Baseline.

98% Widely available
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
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
whatToShow Excellent

Bottom line: Safe for modern browsers. Set the mask at create time; use filter for finer screening. Avoid legacy SHOW_* constants that no longer take effect.

Frequently Asked Questions

A read-only non-negative integer—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. A few NodeFilter.SHOW_* constants are legacy and no longer effective.
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). filter is a custom acceptNode callback for finer screening—for example only certain elements.
When you omit the second argument to createNodeIterator, the default is NodeFilter.SHOW_ALL (0xFFFFFFFF).
Use bitwise OR, for example NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT. Test a flag later with (it.whatToShow & NodeFilter.SHOW_TEXT) !== 0.

Next: detach()

Learn why NodeIterator.detach() is deprecated and what to do instead.

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.

9 people found this page helpful