Two-way conversion between text and HTML entities — prevent injection, handy for web pages and email templates. 100% client-side, data never leaves your browser.
HTML entities come in three forms, all of which render identically:
| Form | Example | Notes |
|---|---|---|
| Named | & | Readable, but only about 2,000 characters have names |
| Decimal | & | Works for any Unicode code point |
| Hexadecimal | & | Same coverage, more compact for high code points |
Named entities are friendlier to read and edit; numeric forms are universal. Either way, the trailing semicolon is required.
Three characters have structural meaning in HTML and must be escaped whenever you want them displayed as text:
< starts a tag. Unescaped, it may swallow the rest of the document.> closes a tag.& introduces an entity, so a literal ampersand can be misread as the start of one.Quotes matter too inside attributes: escape " in double-quoted attributes and ' in single-quoted ones.
Converting < to < renders HTML inert, which prevents many cross-site scripting attacks — but it is not sufficient on its own. Context matters: inside a JavaScript string, inside a URL, or inside a CSS value, the dangerous characters are different. Escape according to where the value is being placed, and prefer framework-provided escaping over hand-written replacements.
| Character | Named entity | Numeric |
|---|---|---|
| Non-breaking space | |   |
| Ampersand | & | & |
| Less-than | < | < |
| Copyright | © | © |
| Em dash | — | — |
| Euro sign | € | € |
A non-breaking space looks like a space but stops a line break at that point — useful between a number and its unit, or inside a product name you do not want split.
Escaping is only correct once, at the moment a value is written into HTML. If the stored text already contains an escaped ampersand — Tom & Jerry — and a template escapes it again, the result is Tom &amp; Jerry and the visitor sees the literal characters & on the page instead of an ampersand.
This shows up when the same value passes through several layers: a rich-text editor, a database round-trip, an API response, then a template. The fix is a rule rather than a patch — keep raw characters in storage, escape exactly once on output, and never escape in a layer that only forwards data.
<script> and <style> are raw-text elements: the HTML parser does not decode entities inside them, so writing & in a script hands JavaScript the five characters &, not an ampersand. Build strings there with ordinary characters and leave entity encoding to markup.
Inside attributes the rules are the same as in text, with one addition — whichever quote character delimits the attribute must be escaped, or it terminates the value early. Using single quotes around an attribute that contains a double quote avoids the problem entirely.
HTML5 tolerates a missing semicolon in some positions, but the parser then takes the longest entity name it can match. That turns ¬it; into ¬ followed by it;, which renders as ¬it; instead of the intended text. Numeric forms are even riskier without a terminator, because the parser keeps consuming digits. Write the semicolon every time and the ambiguity disappears.