TechCompare LogoTechCompare

HTML entity encoding: when to use &, <, and Unicode escapes

Always entity-encode user-supplied text in HTML. Let your framework do it automatically. Only use named/numeric entities for literal characters you're writing yourself in static HTML. For emoji and non-ASCII text, use UTF-8 directly rather than numeric entities.

HTML entity encoding replaces characters that have special meaning in HTML with named or numeric entities: < becomes &lt;, > becomes &gt;, & becomes &amp;, and double-quote becomes &quot;. This is a security requirement, not a style choice. Without it, user-supplied text containing <script> tags will execute in the browser.

By TechCompare ยท Updated

Encoding focus
HTML entity encoding
html-entities
Category
Best Practices
Practical encoding guidance

How this is calculated

The five characters that must always be entity-encoded in HTML text content are ampersand, less-than, greater-than, single-quote, and double-quote. Modern frameworks (React, Vue, Svelte) do this automatically when you use their templating syntax. The risk surfaces when you use dangerouslySetInnerHTML, innerHTML in vanilla JS, or server-side template engines that don't auto-escape. For UTF-8 characters beyond ASCII, you can use them directly in HTML source (no entity needed) as long as the page declares <meta charset='utf-8'>. Numeric entities like &#x1F600; (๐Ÿ˜€) are a fallback for environments where the source file encoding is uncertain.

Verdict

Five characters gate the XSS surface: ampersand, less-than, greater-than, single-quote, and double-quote. React, Vue, and Svelte handle the encodings automatically through their templating syntax, so the risk only surfaces with dangerouslySetInnerHTML, raw innerHTML, or non-escaping server engines. For non-ASCII characters including emoji, declare <meta charset='utf-8'> and ship them directly instead of leaning on numeric entities like &#x1F600;.

More Encoding scenarios

Base64 vs Hex
Base64 and hexadecimal both encode binary data as text, but they serve different purposes.
View details โžœ
UTF-8 vs ASCII
ASCII maps 128 English characters to 7-bit values.
View details โžœ
URL encoding guide
Percent-encoding (also called URL encoding) replaces characters that aren't safe in a URL with a percent sign followed by two hex digits.
View details โžœ

Frequently asked questions

Which characters must be entity-encoded in HTML?
Five: ampersand becomes &amp;, less-than becomes &lt;, greater-than becomes &gt;, double-quote becomes &quot;, and single-quote becomes &#39;. Leaving them raw in user-supplied text is how XSS attacks slip <script> tags into a page, so this is a security rule, not formatting preference.
Does React escape HTML entities automatically?
Yes, and Vue and Svelte do too. Interpolated expressions in JSX are escaped by default, so a user comment containing <script> renders as text. The protection vanishes the moment you use dangerouslySetInnerHTML, raw innerHTML in vanilla JS, or a server template marked safe. Those code paths are where entity handling is your job.
Should I use HTML entities for emoji and accent characters?
No, use UTF-8 directly. As long as the page declares <meta charset="utf-8">, characters like รฉ or ๐Ÿ˜€ can live as literal text in your HTML. Numeric entities like &#x1F600; are a fallback for environments where the file encoding is unreliable, and they make source harder to read than the characters they stand for.