JavaScript: textContent

The textContent property reads or replaces an element’s plain text without parsing the value as HTML.

What you will learn

Minimal example

const message = document.querySelector('#message');
const name = 'A <visitor>';
if (message) message.textContent = `Hello, ${name}`;

The characters are displayed as text, not interpreted as an element or script. Use innerHTML only when you intentionally create trusted markup and understand the escaping requirements.

Common mistakes