Skip to content

Menu

Unicode Cheat Sheet: Code Points, UTF-8, Escapes and Normalisation

Checked against Unicode Unicode 18.0.0 (UAX #15 rev 58, UAX #29 rev 49); examples run on Node 24.4.0 and Python 3.12.8 on 28 November 2026. Each section links to the official docs it was checked against.

This sheet is for developers who have been bitten by a string that looks right but compares wrong, counts wrong or turns into é on the way out. It follows Unicode 18.0.0, released on 16 September 2026, and the normalisation and segmentation annexes reissued for it. Every JavaScript and Python result below was captured on Node 24.4.0 and Python 3.12.8.

When to use what

Keep three layers apart in your head. A code point is the abstract number, such as U+1F600. An encoding turns it into bytes: UTF-8 uses one to four bytes, UTF-16 uses one or two 16-bit units. A grapheme cluster is what a person calls one character, and it can be many code points. Most bugs come from measuring one layer with a tool built for another.

So pick the counter by the question. Byte limits for a database column, a header or an HTTP body: count UTF-8 bytes with Buffer.byteLength or len(s.encode()). Validating code points or walking them: use [...s] in JavaScript or plain len() in Python. A user-facing limit like "max 20 characters" or a cursor position: count graphemes with Intl.Segmenter. JavaScript's .length answers none of these well; it counts UTF-16 units.

For comparison and storage, normalise to NFC at the boundary where text enters your system. That is what the W3C recommends for content, according to UAX #15. Reach for NFKC only for identifiers, search keys and usernames, where you want the fi ligature and f i to be the same thing; it changes meaning, so never write it back over the original text. When you need to look at an unknown character, paste it into the Unicode lookup; to turn text into escapes for source code, use the Unicode escape tool. Hidden characters such as zero-width joiners show up in the invisible character detector.

MDN describes JavaScript strings as sequences of UTF-16 code units; anything above U+FFFF becomes a surrogate pair. Python's str holds code points, so its len() disagrees with JavaScript for emoji.

"\u{1F600}".length // 2

JavaScript counts the two UTF-16 surrogate units of one emoji, not one character

[..."\u{1F600}"].length // 1

Spread iterates by code point, so a surrogate pair counts once

Buffer.byteLength("\u{1F600}") // 4

Count UTF-8 bytes in Node, which is what a byte-limited column or header cares about

len("\U0001F600".encode("utf-8")) # 4

Count UTF-8 bytes in Python; plain len() on the str gives 1 code point

"\u{1F600}".codePointAt(0).toString(16) // '1f600'

Read the full code point at an index; charCodeAt(0) would give only the lead surrogate d83d

hex(ord("\U0001F600")) # '0x1f600'

Get a character's code point in Python as hex

String.fromCharCode(0x61, 0xD800).isWellFormed() // false

Detect a lone surrogate before sending a string to an API that needs valid UTF-8; toWellFormed() swaps it for U+FFFD

JSON rules come from RFC 8259 section 7. HTML forms are from MDN's character reference glossary page and CSS from MDN's <string> page. The HTML and CSS rows were checked in the docs only.

"\u00E9" "\u{1F600}"

JavaScript escapes: exactly four hex digits for a BMP unit, or braces with one to six digits for any code point

"\u00e9" "\U0001F600" "\N{EM DASH}"

Python escapes: four digits, eight digits for astral code points, or the character's official name

"\uD83D\uDE00"

JSON has no long escape, so a character above U+FFFF is written as its UTF-16 surrogate pair

json.dumps(s, ensure_ascii=False)

Stop Python from turning every non-ASCII character into \u escapes when writing JSON; the default output was "\u00e9\ud83d\ude00"

&eacute; &#233; &#xE9;

HTML named, decimal and hex character references for the same character

Try it: HTML Entity EncoderFrom the docs, not run by us
content: "\2014";

CSS string escape: a backslash then the hex code point, here an em dash

From the docs, not run by us

UAX #15 warns that the K forms must not be applied blindly to arbitrary text, and that joining two normalised strings can give a result that is no longer normalised.

"\u00E9" === "e\u0301" // false

Precomposed and decomposed e-acute look identical but are different code point sequences

a.normalize() === b.normalize()

Compare after normalising to NFC, the default form for String.prototype.normalize; the e-acute pair becomes true

"caf\u00E9".normalize("NFD").length // 5

Split precomposed letters into base plus combining mark, handy before stripping accents

"\uFB01".normalize("NFKC") // 'fi'

Fold compatibility characters such as ligatures, circled digits and superscripts to plain forms for search keys

unicodedata.normalize("NFC", s)

Python equivalent; the form name is the first argument

unicodedata.is_normalized("NFC", s)

Check a string without building a copy (Python 3.8+); returned False for e plus U+0301

UAX #29 keeps emoji ZWJ sequences together (rule GB11) and pairs regional indicators into flags (GB12, GB13). Intl.Segmenter is documented at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter.

new Intl.Segmenter("en", { granularity: "grapheme" })

Build a segmenter that splits text the way a cursor moves; word and sentence granularities also exist

[...seg.segment(text)].length

Count user-perceived characters; family emoji + e-acute (decomposed) + a flag gave 3, while .length gave 14

[..."\u{1F468}\u200D\u{1F469}\u200D\u{1F467}"].length // 5

Spread splits a ZWJ family emoji into three people and two joiners, so it is not a grapheme counter

s.split("").reverse().join("")

Naive reverse that tears surrogate pairs apart; an emoji came back as the broken pair U+DE00 U+D83D

/^.$/u.test("\u{1F600}") // true

Add the u flag so a regex dot matches a whole code point; without it the test was false

RFC 8259 section 8.1 says senders must not add a BOM to networked JSON, though parsers may ignore one.

b"\xef\xbb\xbfA".decode("utf-8-sig") # 'A'

Decode UTF-8 and drop a leading BOM; the plain utf-8 codec kept it as '\ufeffA'

new TextDecoder().decode(bytes)

Browser and Node decoder strips a UTF-8 BOM by default; with { ignoreBOM: true } the BOM stayed and the length went from 1 to 2

"\U0001F600".encode("utf-16")

Python's utf-16 codec writes a BOM first (b'\xff\xfe' on little-endian); use utf-16-le or utf-16-be to leave it out

unicodedata.name("\u00e9")

Get the official name, here LATIN SMALL LETTER E WITH ACUTE

unicodedata.lookup("SNOWMAN")

Go the other way, from a name to the character

unicodedata.category("\u00e9") # 'Ll'

Read the two-letter general category, useful for filtering letters, marks or punctuation

Worked example: Make a username check agree with what the user sees

A signup form rejected josé as "already taken" for one user and accepted it for another. The stored name had been typed on a Mac keyboard layout that produced a decomposed accent. Here is the check, run on Node 24.4.0:

const stored = "jose\u0301";      // e + combining acute
const typed  = "jos\u00e9";       // precomposed e-acute
console.log(stored === typed);                         // false
console.log(stored.length, typed.length);              // 5 4
const key = (s) => s.normalize("NFKC").toLowerCase();
console.log(key(stored) === key(typed));               // true
const seg = new Intl.Segmenter("en", { granularity: "grapheme" });
console.log([...seg.segment(stored)].length);          // 4

The raw comparison fails because the two strings hold different code points, and even their lengths differ. Normalising both to NFKC and lowercasing gives a lookup key that treats them as the same name. Keep the key in a separate indexed column and leave the display name as the user typed it. The last line shows why a "max 20 characters" rule should count graphemes: the decomposed name is 5 units long but the user sees 4 letters.

Common mistakes

  • Enforcing a character limit with .length in JavaScript.

    Count graphemes with Intl.Segmenter for limits people see, and UTF-8 bytes with Buffer.byteLength for storage limits.

  • Comparing or indexing user text without normalising it first.

    Normalise to NFC when text enters the system, and compare normalised forms; macOS input and copy-paste often produce NFD.

  • Saving the NFKC form over the original text.

    Use NFKC only for derived keys such as usernames or search terms, because it turns superscripts, ligatures and circled digits into plain characters for good.

  • Writing a UTF-8 BOM at the start of JSON or CSV meant for other programs.

    Write plain UTF-8; RFC 8259 forbids a BOM on networked JSON, and when you read files that may have one, decode with utf-8-sig in Python.

  • Truncating a string with slice at a fixed unit count.

    Cut on grapheme boundaries from Intl.Segmenter, otherwise you can split a surrogate pair and send a lone surrogate that encodeURI rejects.