Open a page in view source and you will find & scattered everywhere, plus a pile of   used as spacing. Both are legal. Only one of them is usually necessary.

HTML does not require you to encode much. Inside ordinary text, two characters need attention: & and <. In a double-quoted attribute, add ". That is nearly the entire job. Every other character can be typed directly as long as the document declares UTF-8.

The five, with reasons

& starts an entity reference. A bare ampersand in text is usually tolerated by browsers, but it stops being tolerated the moment the text contains something that looks like &copy; or &amp;, and then you get a glyph you did not ask for. Escape it.

< starts a tag. This one is not optional; a stray < in text is a parse error waiting to happen, and in a user-provided string it is how injection starts.

> ends a tag. Technically tolerable in text, always escaped by encoders because ambiguity is expensive later.

" ends a double-quoted attribute. ' ends a single-quoted one. Escape whichever quote delimits the attribute you are writing, not both.

&apos; is valid in XML and HTML5 but was absent from HTML 4. If something downstream still runs an HTML 4 parser, write &#39; instead.

Named, decimal, hex

&copy;, &#169; and &#xA9; all produce the same glyph. Named entities read better in source. Numeric ones are unambiguous and exist for every code point, which is why encoders often emit them by default.

The names I actually use: &nbsp;, &mdash;, &ndash;, &hellip;, &copy;, &times;, &euro;, and the curly quotes &ldquo; &rdquo; &lsquo; &rsquo;. Everything else I write as a literal character.

Context changes the rule

Text nodes: escape & and <.

Double-quoted attributes: escape &, < and ".

Single-quoted attributes: escape &, < and '.

Unquoted attributes: don't. The escape set becomes hard to reason about and one missing quote ends the attribute early.

<script> and <style>: entities are not decoded here at all, so HTML encoding produces visible garbage. What matters instead is stopping </script> from appearing inside a JSON string, because the HTML tokenizer will end the block there regardless of JavaScript quoting. Escape that sequence as \u003c/script\u003e when you inline data.

<textarea> and <title>: entities are decoded, but a literal closing tag still terminates the element, so < needs escaping there too.

A single blanket escapeHtml() applied to every sink gets at least one of these wrong. This is why context-aware escaping exists in every serious template engine.

Double encoding, in both directions

The visible symptom is &amp;lt; on screen, or Tom &amp;amp; Jerry after an edit. The cause is encoding twice: once when the value entered storage, once when it left.

The mirror bug is worse. Decode on input, store the decoded value, render it unescaped, and a <script> tag typed into a comment box becomes script on the page. Encode at output. Store raw. Keep the decision next to the sink that needs it.

If a legacy system already stores encoded text, mark those rows and never re-encode them. Retrofitting that distinction later is painful, which is an argument for doing it now.

Not the same as URL encoding

Four different escape systems get confused constantly.

Layer Example Job
HTML entity &amp; A character in HTML source
Percent encoding %26 A character inside a URL component
JSON escape \u0026 A character inside a JSON string
JS escape \x26 A character inside a JS string literal

A search URL with two parameters contains & separators, and if that URL sits in an HTML attribute, those separators may need to be &amp;. Both conversions are correct in the same line of markup. Doing only one is the mistake I see most often in bug reports.

  is punctuation, not layout

A non-breaking space prevents a line break. It renders at the width of a space in the current font, which means it is not a unit of layout. A row of them used as indentation collapses differently across fonts, text zoom levels and phone rendering.

Worth using for a number and its unit (10&nbsp;kg) or two words that must stay together in a narrow column. Not worth using for centring, table padding or fake indentation. Use CSS for those. Screen readers also handle long runs of &nbsp; inconsistently, and copying the text produces invisible characters that break search and comparison.

Before you ship

Encode once, at output. Check the value that already contains &amp;. Test the four boundaries: ampersand, angle bracket, quote and apostrophe. And confirm that the page is served as UTF-8, otherwise literals like é and € arrive as mojibake no matter how carefully you escaped everything else.

The HTML entity encoder converts both directions if you want to see exactly what a string becomes, and the URL encoder handles the query-string case.