innerText Is Layout Aware, textContent Is Not
textContent concatenates every text node including style rules and hidden spans, while innerText approximates rendered output and drops what CSS hides.
Code
document.body.innerHTML = `
<div id="box">
<style>.gone { display: none; }</style>
<p>Alpha <span class="gone">hidden</span> Beta</p>
</div>
`;
const box = document.querySelector("#box");
// textContent reads every text node in the tree, including
// the style sheet text and the display:none span
console.log(box.textContent.includes(".gone")); // true
console.log(box.textContent.includes("hidden")); // true
// innerText approximates rendered text: what CSS hides
// does not appear
console.log(box.innerText.includes(".gone")); // false
console.log(box.innerText.includes("hidden")); // falseThe route the code takes
Top-level bindings box hold the demo state — nothing sits inside a function scope.
What to watch for
- The environment needs no preparation beyond having the interpreter or compiler.
documentexist only in browsers — a Node runtime has no shims for them out of the box.
Core lines
<p>Alpha <span class="gone">hidden</span> Beta</p>
</div>
`;
const box = document.querySelector("#box");
Interactive Tools for This Topic
Accept Header ParserParse an Accept header into media ranges with their quality values and specificity, sorted by preference for content negotiation debugging.CSS Flexbox PlaygroundInteractively test CSS flexbox container and item properties with a live preview. Adjust justify-content, align-items, flex-direction, and flex-grow to see layout changes.CSS Grid PlaygroundExperiment with CSS grid layout properties interactively. Define columns, rows, gaps, and areas while watching the grid template update in a live visual preview.DOM Event Propagation VisualizerVisualize DOM event capturing, targeting, and bubbling phases in real time. Toggle stopPropagation and stopImmediatePropagation to see how events traverse the tree.
FAQ
- Does innerText Is Layout Aware, textContent Is Not run in plain Node.js?
- No — it touches `document`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.
Report a problem with this exampleOpens a pre-filled issue on GitHub.