innerText vs textContent in JavaScript – Not Twins, Just Cousins!
JavaScript
DOM
Web APIs

innerText vs textContent in JavaScript – Not Twins, Just Cousins!

Learn the key differences between innerText and textContent in JavaScript DOM manipulation. Understand how they work, when to use them, and why it matters—with real-world examples and use cases.
2 min read

In JavaScript DOM, both innerText and textContent seem to give you the text inside an HTML element. But under the hood, they work quite differently.

Let’s explore what they are, how they differ, and when to use which—with real-world examples!

The textContent — The Raw Texter

  • Returns all the text in the element, including hidden elements.
  • Ignores CSS and layout.
  • Faster because there are no layout calculations.
  • Great for getting raw data or sanitizing content.

Let's create a hidden text on the UI:

HTML
<div id="info">
  Hello <span style="display:none">World</span>
</div>

Now, if you use the textContent property:

JS
document.getElementById("info").textContent;
// Output: "Hello World"

You will get "Hello World" even though “World” is hidden.

Use textContent when you need all content, regardless of visibility.

The innerText — The Visible Speaker

  • Returns only the visible (rendered) text.
  • Respects CSS like display: none, visibility: hidden, etc.
  • Slower (causes layout reflow).
  • Ideal for mimicking what users actually see on screen.

For, the same HTML above, if you use the innerText:

JS
document.getElementById("info").innerText;
// Output: "Hello"

You will only get the “Hello”. The hidden “World” is ignored.

Use innerText when you want screen-accurate content (what the user sees).

You can learn about DOM Manipulations in JavaScript with examples and projects from this session.

Final Thoughts

Often developers use the innerText and textContent interchangibly without understanding their real purposes. Please note:

  • If you’re reading or saving data, go for textContent.
  • If you’re testing, displaying, or analyzing visible UI, use innerText.
  • They’re not interchangeable—choose wisely based on context!

Join 40 Days of JavaScript for FREE

There are 101 ways of learning something. But, nothing can beat the “structured” and “progressive” learning methodologies. I have designed this FREE task-based Course for you to stay structured and motivated. Sounds great? Please take a look:

Join 40 Days of JavaScript Course for FREE - Do not miss the chance to learn JavaScript in-depth with practical projects and assignments. JavaScript is omnipresent and learning the language well will help you get a better grip on many other libraries and frameworks like ReactJS, Angular, Next.js, Node.js, Remix, and many more. So, make sure to join the 40 Days of JavaScript initiative and master the language.

Learn Full Stack

with tapaScript

By clicking Sign Up you re confirming that you agree with our Terms and Conditions.

newsletter

Don't forget to connect with us on