Unicode in HTML, CSS, JavaScript, and JSON: Choosing the Right Character Escape
UnicodeHTMLCSSJavaScriptJSONWeb Development

Unicode in HTML, CSS, JavaScript, and JSON: Choosing the Right Character Escape

UUnicode.live Editorial Team
2026-11-13
7 min read

Choose safe Unicode syntax for HTML, CSS, JavaScript, and JSON, with checklists for escapes, encoding errors, and rendering problems.

Unicode characters can move through templates, stylesheets, JavaScript, APIs, and configuration files in several different forms. This guide gives you a practical way to choose between literal characters, HTML entities, CSS escapes, JavaScript code-point escapes, JSON escapes, and UTF-8 source text—then troubleshoot the common failures that appear when text crosses those boundaries.

Overview

A Unicode code point identifies a character, but each language gives you its own syntax for representing that character. The right choice depends on the file format and the place where the value will be interpreted. An HTML document, a CSS declaration, a JavaScript string, and a JSON payload do not share identical escape rules.

For example, the grinning face character can be written literally as 😀, represented in HTML as 😀, in CSS as \1F600, in JavaScript as \u{1F600}, or in JSON as \uD83D\uDE00. These forms may produce the same visible result, but they are not interchangeable source syntax.

As a default, use a UTF-8 source file and write a literal character when your editor, repository, build system, and deployment path preserve UTF-8 reliably. Use an escape when the surrounding syntax requires it, when a control character must be represented safely, or when an escaped form makes a source file clearer and more portable. Do not use an escape merely because a character is non-ASCII.

ContextExample for U+1F600Key rule
Literal UTF-8 source😀The file and every processing step must preserve UTF-8.
HTML&#x1F600;Use a numeric character reference; escape context-sensitive characters such as & and < where needed.
CSS\1F600CSS hexadecimal escapes allow up to six hex digits; add whitespace when the next character could be read as part of the escape.
JavaScript\u{1F600}Code-point escapes require the braced form; the older form uses UTF-16 surrogate pairs.
JSON\uD83D\uDE00JSON uses four-hex-digit \uXXXX escapes, so non-BMP characters are commonly represented by a surrogate pair.

Checklist by scenario

Putting a character in HTML

  1. Decide whether a literal UTF-8 character is appropriate for the document and repository.
  2. If using an entity, choose a numeric reference such as &#x00A9; for © or a named reference when a well-known name improves readability.
  3. Check the context. Text content, an attribute value, a URL, and an inline script have different escaping concerns.
  4. Escape markup-significant characters, especially ampersands and angle brackets, rather than relying on a Unicode escape to make unsafe markup safe.
  5. Verify the response encoding and the document's declared character encoding if the browser displays replacement characters or mojibake.

HTML numeric references are interpreted by the HTML parser. They are not a general-purpose encoding for every value inserted into a page. Use context-aware escaping or a framework's standard rendering mechanism for dynamic data.

Adding a character in CSS

  1. Use a literal character when the stylesheet is reliably UTF-8 and the glyph is easy for maintainers to identify.
  2. For content, pseudo-elements, or generated labels, use a CSS escape such as content: "\2605"; for ★.
  3. Remember that CSS escapes are hexadecimal and can contain up to six digits. If a following character is a hexadecimal digit, terminate the escape with whitespace: \26 0A is different from an unseparated sequence.
  4. Check whether the selected font contains the glyph. A correct CSS escape cannot create a glyph that the font does not provide.
  5. Test generated content with screen readers and localization requirements; an icon or decorative mark may belong in markup or an accessible label instead.

Writing a Unicode character in JavaScript

  1. Use a literal character in a UTF-8 JavaScript source file when it improves readability.
  2. Use \u{...} for a code point, for example const smile = "\u{1F600}";.
  3. Use the four-digit form, such as \u00A9, for characters within that range. Do not write \u1F600 and assume it means U+1F600; without braces, JavaScript reads only four hexadecimal digits.
  4. When compatibility with older syntax or a particular serialized format requires it, represent a supplementary character with its UTF-16 surrogate pair, such as "\uD83D\uDE00".
  5. Test operations involving length, slicing, and indexing. JavaScript string indexes address UTF-16 code units, not necessarily user-perceived characters.

For matching scripts or character properties, JavaScript Unicode property escapes may be more suitable than manually listing ranges. See Unicode Property Escapes in JavaScript Regex when the task is classification rather than displaying a character.

Putting Unicode in JSON

  1. Use valid JSON string syntax, including double quotes and JSON's limited escape set.
  2. Remember that JSON supports \u followed by exactly four hexadecimal digits. The JavaScript-style \u{1F600} form is not a JSON escape.
  3. For a supplementary code point, use a valid surrogate pair when escaping it, or include the literal UTF-8 character if the JSON transport and parser support it.
  4. Let a JSON serializer produce escapes and quoting where possible. Avoid hand-building JSON with string concatenation.
  5. Validate the complete document with a JSON parser, not just a syntax highlighter or text editor.

JSON escaping and URL encoding solve different problems. If a JSON value is later placed in a URL, it may need URL encoding at that separate boundary. Similarly, Base64 is an encoding of bytes, not a replacement for Unicode-aware escaping.

What to double-check

Encoding versus escaping

UTF-8 is an encoding that maps Unicode text to bytes. An HTML entity, CSS escape, JavaScript escape, or JSON escape is source notation interpreted by a particular parser. Converting a character to an escape does not automatically fix a byte-decoding problem. If text appears as sequences such as é instead of é, inspect where bytes were decoded with the wrong character encoding before changing application-level escapes.

Code points, code units, and grapheme clusters

A visible symbol may consist of multiple code points, and a single code point above U+FFFF may occupy two UTF-16 code units in JavaScript. Emoji sequences can contain modifiers, variation selectors, or joined characters. Therefore, a visual character is not always equivalent to one code point, one byte sequence, or one JavaScript index. For deeper debugging, review Unicode Code Points vs UTF-8 and UTF-16 and Emoji, Grapheme Clusters, and Unicode-Aware String Length.

Normalization and presentation

Two strings can look identical while using different underlying sequences, such as a precomposed character versus a base character followed by a combining mark. Normalize at an intentional application boundary when comparison or lookup requires it; do not assume that escaping changes normalization. The Unicode Normalization guide covers the practical differences between NFC, NFD, NFKC, and NFKD.

Fonts, direction, and variation

If the source value is correct but the result is a box, tofu glyph, or unexpected presentation, inspect font coverage and rendering. For multilingual interfaces, also check bidirectional behavior and line wrapping. Variation selectors can affect emoji or text presentation, while right-to-left text can change visual order without changing the stored string. The related guides on variation selectors, bidirectional text, and line breaking provide focused checklists.

Common mistakes

  • Using JavaScript syntax in JSON: Replace \u{1F600} with a literal character or a valid pair of four-digit JSON escapes.
  • Forgetting CSS escape termination: A following hexadecimal character can be consumed as part of the escape. Add a space or use a literal character.
  • Escaping the wrong layer: HTML escaping, JavaScript string escaping, JSON escaping, and URL encoding should be applied for the parser that will consume the value next.
  • Confusing a missing glyph with corrupt text: A square box often indicates font coverage or rendering trouble, whereas mojibake usually points to incorrect byte decoding.
  • Assuming visible equality means string equality: Normalize or compare at the intended Unicode level when combining marks or compatibility characters are involved.
  • Hand-editing serialized payloads: Generate JSON with a serializer and inspect the parsed value as well as its wire representation.
  • Treating escaped text as trusted text: An escape sequence can represent data safely in one context but become unsafe when copied into another without context-specific handling.

When to revisit

Keep this checklist close whenever text moves between a template, stylesheet, script, API response, database, test fixture, or configuration file. Revisit it before a major framework, bundler, serializer, editor, or runtime change, because a tool may alter default serialization or source-transformation behavior. It is also worth checking before seasonal planning cycles when teams refresh templates, localization files, content catalogs, or automated test data.

When a Unicode issue appears, use this short workflow:

  1. Copy the failing value into a controlled UTF-8 text file without retyping it.
  2. Identify the intended code point or sequence, including variation selectors and combining marks if relevant.
  3. Record the boundary where the value first changes: editor, build step, HTTP response, parser, database, or UI renderer.
  4. Validate the syntax for that specific boundary rather than applying a familiar escape from another language.
  5. Inspect bytes, decoded text, parsed values, and rendered output separately.
  6. Test at least one supplementary character, one combining sequence, and one context-sensitive character if the workflow handles general user text.
  7. Document the chosen representation in the repository or API contract so the next change does not restart the investigation.

The safest Unicode workflow is not to escape everything. It is to preserve UTF-8 deliberately, use the notation required by each parser, validate at every boundary, and distinguish encoding failures from font and rendering failures.

Related Topics

#Unicode#HTML#CSS#JavaScript#JSON#Web Development
U

Unicode.live Editorial Team

Technical Editor

Senior editor and content strategist. Writing about technology, design, and the future of digital media. Follow along for deep dives into the industry's moving parts.