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.

4 min read

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:

textContentinnerText
Hidden text (display: none)IncludedExcluded
Script and style contentIncludedExcluded
PerformanceFast, no layout neededSlower, triggers reflow
Whitespace formattingAs-is from HTMLNormalized like browser rendering
Setting textPlain text, no HTMLPlain 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.

htmlhtml
<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:

javascriptjavascript
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:

javascriptjavascript
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:

javascriptjavascript
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:

javascriptjavascript
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

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.
RunePowered by Rune AI

Frequently Asked Questions

Which is faster, innerText or textContent?

textContent is significantly faster because it returns text directly from the DOM without checking CSS. innerText triggers a layout recalculation to determine which text is visible, making it slower, especially in loops or on pages with complex styles.

Does innerText include text from hidden elements?

No. innerText only returns text that is rendered and visible to the user. Elements hidden with display: none or visibility: hidden are excluded. textContent returns everything regardless of visibility.

Can I set innerText to update page text?

Yes, but textContent is preferred for setting text. Both accept a string and treat it as plain text, but textContent is faster because it skips the CSS layout step. Use innerText only when the rendering-aware behavior matters.

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.