Short answer
Decoding HTML entities means letting the parser turn character references into the characters the spec names. A reference starts with an ampersand. Named ones use a name from the HTML table and a semicolon. Amp, lt, and gt are in that table. In JavaScript, MDN documents DOMParser parseFromString with the text/html type, then textContent. There is no decodeEntities built-in on those pages.
Last verified against the sources listed below.
Key takeaways
- The HTML spec says a character reference starts with an ampersand and has three forms: named, decimal, and hexadecimal.
- The named table maps amp to U+00026, lt to U+0003C, and gt to U+0003E.
- MDN lists the same three names and says the decimal form of less-than is 60.
- DOMParser with text/html parses a string into a document. Read textContent for the decoded text.
- parseFromString is an injection sink. Keep the result as text unless you have sanitized markup.
How to decode HTML entities is a parser question: a character reference is an ampersand plus a name or a number, and the HTML parser replaces it with the character the spec assigns.
Guessing a replacement list misses names, misses case, and misses the semicolon rules. The named-character table is the list. The parser is the tool that applies it.
HTML is the language those references belong to. What HTML is used for is the broader guide.
What counts as a character reference?
A character reference is an escape that starts with an ampersand and stands for a character the surrounding text could not hold raw.
The syntax section of the HTML Standard, last updated 2 December 2026, says references must start with U+0026 AMPERSAND. It then allows three kinds.
A named reference uses a name from the named character references section, in the same case, terminated by a semicolon. A decimal reference is an ampersand, a number sign, ASCII digits, and a semicolon.
A hexadecimal reference puts x or X after the number sign, then hex digits, then a semicolon.
The numeric forms may point at any code point except U+000D CARRIAGE RETURN, noncharacters, and controls other than ASCII whitespace. An ambiguous ampersand is an ampersand, ASCII alphanumerics, and a semicolon that do not match any name in the table.
MDN’s glossary uses the same three-way split. It says named references put a name between an ampersand and a semicolon, and it gives less-than as the example of a named reference used because the parser treats that character as the start of a tag.
Which named references does the spec define for amp, lt, and gt?
The named-character table maps amp to U+00026, lt to U+0003C, and gt to U+0003E, each with a semicolon form.
The named character references section says the table will not be expanded or changed in the future. It also says many code points have more than one name, including forms with and without the trailing semicolon, for legacy compatibility.
The glyphs in the table are non-normative. The code points are the definition.
| Name in the table | Character | Code point |
|---|---|---|
amp; |
& | U+00026 |
lt; |
< | U+0003C |
gt; |
> | U+0003E |
MDN’s glossary repeats those three and adds the decimal reading: U+0003C is 60 in decimal, so the decimal reference for less-than uses 60. The hex form uses 3C. This guide’s worked case stays on the three named forms with semicolons, which the syntax section requires.
The XML specification defines five predefined entities: amp, lt, gt, apos, and quot. A Wikipedia catalogue lists the same names among HTML and XML references. The HTML table is the normative list for a web page. It says every XML processor must recognize them whether or not they are declared.
It also says a raw ampersand and a raw less-than must not appear in character data, and must be escaped. HTML’s table includes those names too, and the HTML parser, not a private list, is what resolves them in a web page.
A worked string, using only names the HTML table defines:
Tom & Jerry <3 > 2
After the parser resolves the three references, the characters are: Tom, a space, an ampersand, a space, Jerry, a space, a less-than sign, the digit 3, a space, a greater-than sign, a space, and the digit 2. Nothing in that result was guessed. Each name is a row in the table above.
How to decode HTML entities in JavaScript
Parse the string as HTML with DOMParser.parseFromString and the type text/html, then read textContent.
MDN documents parseFromString as the method that parses HTML or XML and returns a document. For HTML, the type is text/html.
The returned document is a separate in-memory tree. MDN’s example parses a short HTML string and reads doc3.body.firstChild.textContent.
That is the method. There is no decodeEntities function on that page or in the HTML sections cited here.
const parser = new DOMParser();
const input = "<p>Tom & Jerry <3 > 2</p>";
const doc = parser.parseFromString(input, "text/html");
const text = doc.body.firstChild.textContent;
text is the paragraph’s text after the HTML parser has handled the references. The wrapper <p> exists so the text sits in one element, which matches the shape of MDN’s example. Do not treat text as a new markup string and feed it back into the page if the original input was untrusted.
MDN warns that parseFromString is an injection sink. With text/html, script elements are marked non-executable and event handlers are not run in that inert document.
If you later insert those nodes into the visible page, handlers can run. MDN’s safer example builds a TrustedHTML value, often with a sanitizer, and passes that instead of a raw string.
For a decode, stop at textContent. Text is not markup.
The textarea page describes a different object. Initial content goes between the tags, not in a value attribute.
In JavaScript, value gets or sets the current plain text, and defaultValue gets or sets the initial text. The page does not say to assign entities to innerHTML and read value.
This guide does not add that step.
What should you not replace by hand?
Do not write a partial map of a few names and call it the HTML table.
The table is static and large, and names are case-sensitive. lt is less-than.
Lt is a different row. A missing semicolon can be a parse error, and in some attribute cases the parser keeps the characters instead of resolving the name.
A hand-rolled replace of & will also break a double-escaped string, where the characters you wanted to show were the reference itself.
If the string is not HTML, do not parse it as text/html just to change a few letters. MDN allows text/xml, application/xml, application/xhtml+xml, and image/svg+xml for XML parsing.
A bad XML string comes back with a parsererror element. Check for that node before trusting the text.
Decoding also does not change images in the document. An image compressor shrinks a picture file.
It does not read character references. Other image tools are the same kind of utility: they edit bytes, not the HTML named-character table.
Frequently asked questions
Is there a JavaScript function called decodeEntities?
Not on the MDN pages or the HTML spec sections opened for this guide. MDN documents DOMParser.parseFromString, which parses HTML or XML into a document. For HTML, the type string is text/html. The decoded characters are then on the document, and the examples read them with textContent. A textarea has a value property, which MDN describes as the control's text, not as a decoder function. Inventing a built-in name will fail in the browser.
Do amp, lt, and gt need a semicolon?
The syntax section says a named character reference's name must be one that is terminated by a semicolon, and the name must match the table in the same case. The named-character table also lists amp, lt, and gt without a semicolon, and the spec says many names exist with and without that semicolon for legacy compatibility. The parser treats a missing semicolon as a parse error in the named-character-reference state, except for a special attribute case. Prefer the semicolon form the syntax section requires.
What is the decimal reference for a less-than sign?
MDN's character-reference glossary says the decimal form starts with an ampersand, a number sign, ASCII digits, and a semicolon. It says the less-than sign is U+0003C, and that 3C hexadecimal is 60 in decimal, so the decimal reference uses 60. The hexadecimal form uses 3C after the number sign and an x. Both are numeric character references in the HTML spec. Named lt is the same character. The numeric forms may not reference a carriage return, noncharacters, or controls other than ASCII whitespace.
Does a textarea decode entities by itself?
MDN's textarea page says the element's initial content sits between its tags, and that in JavaScript the value property gets or sets the current text. It does not document assigning a string of entities to innerHTML and reading value back. That pattern is not the method this guide uses. DOMParser is the method MDN actually shows for parsing a string as HTML. If you only need the characters, read textContent and do not insert the parsed nodes into the live page.
Why can the same string stay encoded?
The HTML parser resolves a reference when it is parsing HTML where references are allowed. A JavaScript string is just characters until something parses it. If you print the string in a context that does not parse HTML, the ampersand and the name stay visible. XML has its own rules: the XML spec says processors must recognize the predefined entities amp, lt, gt, apos, and quot. Parsing with DOMParser as text/html follows the HTML rules, not a hand-written replacement list.
Sources
- Character references · HTML Standard, 2026
- Named character references · HTML Standard, 2026
- Character reference · MDN, 2026
- DOMParser.parseFromString() · MDN, 2026
- The textarea element · MDN, 2026
- XML predefined entities · W3C, 2026
- List of XML and HTML character entity references · Wikipedia, 2026