This is my favourite way of decoding HTML characters. The advantage of using this code is that tags are also preserved.

function decodeHtml(html) {
    var txt = document.createElement("textarea");
    txt.innerHTML = html;
    return txt.value;
}

Example: http://jsfiddle.net/k65s3/

Input:

Entity:&nbsp;Bad attempt at XSS:<script>alert('new\nline?')</script><br>

Output:

Entity: Bad attempt at XSS:<script>alert('new\nline?')</script><br>
Answer from Rob W on Stack Overflow
🌐
Reddit
reddit.com › r/learnjavascript › how to decode html entities in javascript?
r/learnjavascript on Reddit: How to decode HTML entities in JavaScript?
May 19, 2021 -

I'm running a small database. As we all know, it's important to sanitize database input to prevent SQL injection. Therefore, I convert e.g ' and " to their corresponding HTML entities, e.g. &#039; for '.

This works fine when displaying text on a web page. Only now I need to show these data dynamically using JavaScript. How do I convert HTML entities to characters in JavaScript?

I've found a few workarounds, for example creating a hidden textarea and copypasta to a from it, but that seems wrong. Can it really be true that there is no such function in JavaScript?

🌐
Teleport
goteleport.com › home › resources › tools › html decoder | instant convert html entity to text
HTML Decoder | Instant Convert HTML Entity to Text | Teleport
Client-side decoding happens in the browser, typically via JavaScript's decodeURIComponent() function. This is used to decode URL parameters and other values retrieved from the page URL.
🌐
GitHub
github.com › mathiasbynens › he
GitHub - mathiasbynens/he: A robust HTML entity encoder/decoder written in JavaScript. · GitHub
he (for “HTML entities”) is a robust HTML entity encoder/decoder written in JavaScript.
Author   mathiasbynens
🌐
W3Schools
w3schools.com › Jsref › jsref_decodeuri.asp
JavaScript decodeURI() Method
The decodeURI() method decodes a URI. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, ...
🌐
Online Tools
emn178.github.io › online-tools › html_decode.html
HTML Decode / Unescape - Online Tools
Decode HTML character references and entities such as <, &, ", and numeric references back to readable characters.
Find elsewhere
🌐
npm
npmjs.com › package › html-entities
html-entities - npm
import {decode} from 'html-entities'; decode('&lt; &gt; &quot; &apos; &amp; &#169; &#8710;'); // -> '< > " \' & © ∆' decode('&copy;', {level: 'html5'}); // -> '©' decode('&copy;', {level: 'xml'}); // -> '&copy;'
      » npm install html-entities
    
Published   Mar 30, 2025
Version   2.6.0
🌐
freeCodeCamp
freecodecamp.org › news › encode-decode-html-base64-using-javascript
How to Encode and Decode HTML Base64 using JavaScript – JS Encoding Example
November 7, 2024 - You can encode a string to base64 in JavaScript using the btoa() function and decode a base64 string using atob() function.
🌐
Mother Eff
mothereff.in › html-entities
HTML entity encoder/decoder
This tool uses he to HTML-encode any string you enter in the ‘decoded’ field, or to decode any HTML-encoded string you enter in the ‘encoded’ field.
🌐
Go Make Things
gomakethings.com › articles › decoding-html-entities-with-vanilla-javascript
Decoding HTML entities with vanilla JavaScript | Go Make Things
January 30, 2018 - var decodeHTML = function (html) { var txt = document.createElement('textarea'); txt.innerHTML = html; return txt.value; }; // Example // Returns "<p>In this course, you'll learn:</p>" var decoded = decodeHTML('&lt;p&gt;In this course, you&rsquo;ll learn:&lt;/p&gt;');
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › HTMLImageElement › decode
HTMLImageElement: decode() method - Web APIs | MDN
November 7, 2025 - Using decode() will delay inserting the image into the DOM until it is fully downloaded and decoded, thereby avoiding the empty image problem:
🌐
Quora
quora.com › How-do-you-decode-HTML-entities-with-JavaScript
How to decode HTML entities with JavaScript - Quora
Quora is a place to gain and share knowledge. It's a platform to ask questions and connect with people who contribute unique insights and quality answers.
🌐
GitHub
gist.github.com › tangoabcdelta › a005f219fbd983656f7ea93b2854afbb
How to decode HTML entities in JavaScript? · GitHub
How to decode HTML entities in JavaScript? GitHub Gist: instantly share code, notes, and snippets.
🌐
Script Asylum
scriptasylum.com › index › html & javascript encoder/decoder - script asylum
HTML & JavaScript Encoder/Decoder - Script Asylum
The same thing is done if you want to encode a whole HTML page, except the encoded part of the string (dark blue) could potentially be HUGE. The escaped function (green) would not change however. I've made a couple of wizards you can use for different purposes. You can achieve the same thing by using the escape/un-escape & encoder/decoder functions above, but these are specialized to take out some of the guesswork. Each of the links below will open a new window. Javascript ...
🌐
Plain English
plainenglish.io › home › blog › javascript › 2 javascript approaches to encode/decode html entities
2 JavaScript Approaches to Encode/Decode HTML Entities
May 17, 2022 - As stated by Vit Tertiumnon in his post: JS: How to decode HTML-entities · There are no JavaScript built-in functionalities to encode or decode HTML entities.
🌐
Deobfuscate
deobfuscate.io
JavaScript Deobfuscator
A simple but powerful deobfuscator to remove common JavaScript obfuscation techniques
🌐
Code Beautify
codebeautify.org › html-decode-string
HTML Decode Online is the Best Tool to Decode HTML String, HTML URL and HTML File.
This HTML decoder is a tool or program that is used to convert HTML-encoded text back into its original, unencoded form. HTML encoding represents characters in HTML documents so they can be appropriately displayed in a web browser.
🌐
Beautifier
beautifier.io
Online JavaScript beautifier
Firefox: Javascript deminifier by Ben Murphy, to be used together with the firebug (github), ... Sublime Text 2/3: CodeFormatter, a python plugin by Avtandil Kikabidze, supports HTML, CSS, JS and a bunch of other languages,