Detailed answer
Escape the example and place it in pre and code when formatting matters.
When inserting text with JavaScript, textContent treats it as text rather than markup. The page around the example still needs valid structure. Do not execute user code merely to show the code's source in a discussion.
Example
<pre><code><h1>Hello HTML</h1></code></pre>
Understanding the behavior
Pre preserves formatting and code marks a code fragment, but neither turns markup into literal text automatically. Escape the source before placing it in the document. Applying escaping repeatedly can also make entities display visibly rather than as the intended characters.
Practical implementation notes
Check the parsed DOM for unexpected elements from the sample. In JavaScript-driven examples, assign textContent so the snippet remains text even when it includes tag-like characters or quotes.
How to check it
Render escaped source in code or assign a string through textContent.
Expected result
The tag appears literally without becoming an active document element.
Watch out: An unescaped closing tag can change the surrounding document.
Read the reference ↗