Why escape `<` to `<`?
Browsers treat `<` as the start of an HTML tag. If user content contains a literal `<`, the parser may interpret it as markup and either break the page layout or, worse, run an injected `<script>` block. Encoding it as `<` makes the browser display the literal less-than character without trying to start a tag.
Is the output safe to inject into innerHTML?
Yes for plain text contexts inside element content. The five-character escape (& < > " ') covers text-node and double-quoted attribute injection. Do not, however, paste escaped output into JavaScript string contexts, URL contexts, or event handlers — those need their own encoding rules. For untrusted input the safest pattern is `el.textContent = userInput`, which performs the equivalent escaping automatically.
Does it escape Unicode?
No — and it should not. UTF-8 HTML pages handle every Unicode character natively, so encoding emoji or non-Latin scripts as numeric entities makes the source larger without improving safety. Only the five characters with special meaning to the HTML parser are escaped.
Why is the ampersand escaped first?
If `<` were escaped before `&`, the resulting `<` would itself contain an ampersand that the next pass would re-escape into `&lt;`. Escaping `&` first guarantees no double-encoding occurs in any character.
When should I escape the single quote (`') vs double quote?
Always escape both. If you are sure your attributes are double-quoted you only strictly need `"`, but escaping both lets the same encoded string be safely dropped into either single- or double-quoted attribute contexts. We use `'` for `'` because the named entity `'` is XML, not HTML4.
Will this break my emoji or Chinese characters?
No. Only the five ASCII characters with HTML meaning are touched. Emoji, Chinese, Arabic, Cyrillic, mathematical symbols — all pass through untouched and render correctly in any UTF-8 document.
Is this the same as `htmlspecialchars` in PHP or `escape` in Lodash?
It produces the same output as PHP `htmlspecialchars($s, ENT_QUOTES)` and Lodash `_.escape`. The five-character set (& < > " ') is the standard XSS-safe escape used by every mainstream framework — React, Angular, Django, Rails — when rendering text into HTML.
Should I escape on input or on output?
On output, every time, in the final encoding context. Escaping at input loses the original data, may double-escape if the same string is rendered into a different context (URL, JSON), and breaks search/edit features. Modern frameworks default to output-time escaping for this reason.