innerHTML reads and writes an element's content as HTML markup: on write, the string is parsed and turned into DOM nodes. textContent reads and writes plain text only: on write, the string becomes a single text node, so <b> shows up literally. Prefer textContent for any text, because it is faster and cannot inject markup. Use innerHTML only with trusted or sanitized HTML.
<p id="p">Hello <b>world</b><span hidden>!</span></p>
const p = document.getElementById('p');
p.innerHTML; // 'Hello <b>world</b><span hidden="">!</span>' (serialized markup)
p.textContent; // 'Hello world!' (all text nodes, including hidden ones)
p.innerText; // 'Hello world' (rendered text only, needs layout)
innerHTML serializes the subtree back to markup. Attribute order and quoting may differ from the source.textContent concatenates every descendant text node, including <script>/<style> contents and hidden elements. It does no layout, so it's cheap.innerText is the third option people forget: it is layout-aware (respects CSS display: none, text-transform, line breaks). Reading it forces a style/layout calculation, which can cause layout thrashing in loops.const name = '<img src=x onerror="alert(1)">';
el.textContent = `Hi ${name}`; // shows the literal string, safe
el.innerHTML = `Hi ${name}`; // creates an <img>, onerror runs → XSS
Both setters remove all existing children. That has side effects:
el.innerHTML += '<li>…</li>' is a classic anti-pattern: it serializes the whole subtree, re-parses it and rebuilds everything, wiping listeners on the existing items. Use insertAdjacentHTML('beforeend', …) or append() instead.
<script> tags inserted via innerHTML do not execute, but event-handler attributes (onerror, onload) and javascript: URLs do. So "scripts don't run" doesn't make it safe.Element.setHTML() (HTML Sanitizer API) and Trusted Types (require-trusted-types-for 'script' in CSP) help enforce this across a codebase.dangerouslySetInnerHTML.textContent = '' is a fast way to clear an element (replaceChildren() is the modern, more explicit alternative).DocumentFragment or with a single innerHTML assignment of trusted template output rather than many incremental writes.textContent = text in, text out; innerHTML = markup parsed into nodes.innerHTML with untrusted input, plus sanitizers and Trusted Types.innerText exists and triggers layout; textContent doesn't.Spotted a mistake or a rights concern? Report an issue