DEV Community

Cover image for Tidbit 03: innerText vs textContent in JavaScript – Not Twins, Just Cousins!
Tapas Adhikary
Tapas Adhikary

Posted on • Originally published at tapascript.io

1

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

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:

<div id="info">
  Hello <span style="display:none">World</span>
</div>
Enter fullscreen mode Exit fullscreen mode

Now, if you use the textContent property:

document.getElementById("info").textContent;
// Output: "Hello World"
Enter fullscreen mode Exit fullscreen mode

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:

document.getElementById("info").innerText;
// Output: "Hello"
Enter fullscreen mode Exit fullscreen mode

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.

Feature flag article image

Create a feature flag in your IDE in 5 minutes with LaunchDarkly’s MCP server 🏁

How to create, evaluate, and modify flags from within your IDE or AI client using natural language with LaunchDarkly's new MCP server. Follow along with this tutorial for step by step instructions.

Read full post

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.

Scale globally with MongoDB Atlas. Try free.

Scale globally with MongoDB Atlas. Try free.

MongoDB Atlas is the global, multi-cloud database for modern apps trusted by developers and enterprises to build, scale, and run cutting-edge applications, with automated scaling, built-in security, and 125+ cloud regions.

Learn More

👋 Kindness is contagious

Explore this insightful write-up embraced by the inclusive DEV Community. Tech enthusiasts of all skill levels can contribute insights and expand our shared knowledge.

Spreading a simple "thank you" uplifts creators—let them know your thoughts in the discussion below!

At DEV, collaborative learning fuels growth and forges stronger connections. If this piece resonated with you, a brief note of thanks goes a long way.

Okay