innerText vs textContent in JavaScript Explained
Learn the key differences between innerText and textContent in JavaScript: visibility awareness, performance, and when to use each property for reading and writing text.
innerText and textContent look similar at first glance. Both properties give you the text inside an element, and both let you replace that text with something new.
They differ in one important way: innerText is aware of CSS and only sees rendered text. textContent sees everything regardless of visibility. This difference affects performance, output, and which one you should choose for any given task.
Here is the core difference in one table:
| textContent | innerText | |
|---|---|---|
| Hidden text (display: none) | Included | Excluded |
| Script and style content | Included | Excluded |
| Performance | Fast, no layout needed | Slower, triggers reflow |
| Whitespace formatting | As-is from HTML | Normalized like browser rendering |
| Setting text | Plain text, no HTML | Plain text, no HTML |
Reading Hidden Text: The Deciding Difference
This is the single most useful test for telling the two properties apart in practice. The clearest way to see the difference is with a hidden element: textContent returns its text, but innerText does not.
<div id="container">
<p>Visible paragraph</p>
<p style="display: none;">Hidden paragraph</p>
</div>The container has two paragraphs, but one is hidden with CSS. Read both properties to see which one includes the hidden text:
const container = document.querySelector("#container");
console.log(container.textContent);
console.log(container.innerText);textContent prints both strings concatenated together. innerText prints only the visible paragraph, skipping the hidden one.
textContent returned the hidden paragraph's text along with the visible one. innerText skipped it entirely because the paragraph is not rendered on screen.
This is the fundamental distinction: textContent reads the DOM tree. innerText reads what the user sees.
Performance: Why textContent Is Faster
textContent is faster because it reads text directly from the DOM without consulting the CSS. innerText must ask the browser's layout engine which text is actually rendered, triggering a recalculation of styles and layout.
In a quick comparison on a page with 1,000 elements, reading textContent on each one completes in a few milliseconds. Reading innerText on the same elements can take ten to a hundred times longer because each read forces the browser to check the current CSS state.
This matters most in loops. Reading textContent on every element skips layout entirely:
const items = document.querySelectorAll(".item");
items.forEach((item) => {
console.log(item.textContent);
});Swapping in innerText for that same loop changes the cost dramatically, because now the browser must recalculate layout on every single iteration to determine what is actually visible:
items.forEach((item) => {
console.log(item.innerText);
});If you are reading text from many elements at once, always use textContent. The performance difference adds up quickly.
What Gets Included
textContent returns the raw text content of an element and all its descendants. This includes text inside script and style tags, hidden elements, and comments. It preserves the whitespace exactly as it appears in the HTML source.
innerText returns only text that would be visible if you selected and copied it from the rendered page. It excludes script and style content, hidden elements, and text that CSS has pushed off-screen. It also normalizes whitespace to match how the browser renders it.
Setting Text: Both Work, textContent Is Preferred
Both properties treat assigned values as plain text. Neither will parse HTML tags:
const element = document.querySelector("#output");
element.textContent = "<strong>Bold</strong>";
// Displays: <strong>Bold</strong> (literal text)
element.innerText = "<strong>Bold</strong>";
// Also displays: <strong>Bold</strong> (literal text)Since both produce the same result when setting text, prefer textContent for its better performance. There is no advantage to using innerText for writes unless the rendering-aware behavior somehow matters for your specific case.
When to Use Which
Use textContent as your default. It is faster, simpler, and returns everything. It is the right choice when you are reading text from elements, building search indexes, extracting data, or setting display text.
Use innerText when you specifically need to match what the user sees on screen. The main use case is when you want to get the visible, rendered text as if the user had selected and copied it, for example, when implementing a "copy to clipboard" feature that should only capture what is visually displayed.
For the broader topic of changing text on a page, see how to change text content using JavaScript DOM. If you need to insert actual HTML markup rather than plain text, see using innerHTML safely.
Rune AI
Key Insights
- textContent returns all text, visible or hidden; innerText returns only rendered text.
- textContent is much faster because it skips CSS layout calculations.
- Both properties treat assigned values as plain text, not HTML.
- Use textContent as the default; reach for innerText only when CSS awareness matters.
- Neither property parses HTML tags; use innerHTML if you need HTML insertion.
Frequently Asked Questions
Which is faster, innerText or textContent?
Does innerText include text from hidden elements?
Can I set innerText to update page text?
Conclusion
innerText and textContent both read and write text, but they work differently under the hood. textContent is faster, returns all text regardless of visibility, and should be your default choice. innerText respects CSS and only returns rendered text, which is useful when you specifically need to match what the user sees on screen.
More in this topic
Using Decorators for Logging in JS Architecture
Learn how JavaScript decorators wrap class methods to add logging without touching the original code, including setup with Babel and how execution order works.
JavaScript While Loop Explained: A Complete Guide
A while loop repeats code as long as a condition stays true. Learn the syntax, see practical examples, and understand when a while loop is the right choice over a for loop.
JavaScript Loops Tutorial: for, while, do while
Loops let JavaScript repeat code without writing it twice. Learn the three core loop types, what each one does, and when to choose one over another.