Unicode & Character Encoding — i18n

Unicode gives every character one code point (U+4E88 for 予); Unicode 17.0 has 159,801 characters. UTF-8 writes a code point as 1 to 4 bytes: Latin 1, Arabic…

A guest from Japan types 予約 ("booking") into the notes field, but Mrs. Sri's dashboard shows "予約". The data isn't broken. The bytes arrived whole and were read with the wrong code, and that's called mojibake. To see how that happens, you need two terms. Unicode gives every character of every script in the world one number, called a code point. An encoding is the way that number is written down as bytes, and on the web it is nearly always UTF-8. Think of Mrs. Sri keeping bookings in a guest book with little boxes. Every letter has a number in one big list (Unicode). In the book, the number is written in a thrifty code: a Latin letter takes 1 box, an Arabic letter 2 boxes, a Japanese character 3 boxes, an emoji 4 boxes (UTF-8). If someone else reads the book with a different code, out come strange letters.

Code points: a number for every character

Each language used to have its own encoding. ASCII has only 128 characters (English letters, digits, punctuation, numbers 0 to 127). Japanese had Shift_JIS, Arabic windows-1256, Western Europe windows-1252. One file couldn't hold Japanese and Arabic at once.

Unicode brings them together: one list for every script. Unicode 17.0 (2025) holds 159,801 characters. A code point is written U+ followed by its number in hexadecimal:

Character Code point Note
R U+0052 A Latin letter, the same number as in ASCII
予 U+4E88 A Japanese character (kanji)
ح U+062D The Arabic letter ha
👨 U+1F468 The man emoji
(invisible) U+200D Zero width joiner (ZWJ), the glue between emoji
'予'.codePointAt(0).toString(16);   // "4e88"
String.fromCodePoint(0x062d);        // "ح"

UTF-8: from numbers to bytes

UTF-8 writes each code point as 1 to 4 bytes. The bigger the number, the more bytes:

Code point range Bytes Examples
U+0000 to U+007F 1 Latin letters, digits (exactly the same as ASCII)
U+0080 to U+07FF 2 Arabic letters, é, the no-break space U+00A0
U+0800 to U+FFFF 3 Japanese and most Chinese characters
U+10000 and up 4 Emoji

We measured it with TextEncoder, which turns text into UTF-8 bytes:

Text UTF-8 bytes (hexadecimal) Count
USD 70.00 (typed) 55 53 44 20 37 30 2E 30 30 9
USD 70.00 from Intl.NumberFormat 55 53 44 C2 A0 37 30 2E 30 30 10
予約 E4 BA 88 E7 B4 84 6
حجز D8 AD D8 AC D8 B2 6
👨‍👩‍👧 F0 9F 91 A8 E2 80 8D F0 9F 91 A9 E2 80 8D F0 9F 91 A7 18

Three ways to measure a text's length

JavaScript strings are stored as UTF-16: each code point becomes one or two 16-bit code units. A code point above U+FFFF (emoji) needs two code units, called a surrogate pair. So there are three different counts, plus UTF-8 bytes. Before you look at the table, guess: the Japanese guest typed 予約, two characters. What is '予約'.length in JavaScript, and how many bytes go to the server?

Text .length (UTF-16 code units) [...s].length (code points) Intl.Segmenter (what you see) UTF-8 bytes
USD 70.00 9 9 9 9
予約 2 2 2 6
حجز 3 3 3 6
👨‍👩‍👧 8 5 1 18

So '予約'.length is 2, but 6 bytes go to the server.

const family = '\u{1F468}\u200D\u{1F469}\u200D\u{1F467}'; // 👨‍👩‍👧
const seg = new Intl.Segmenter('en', { granularity: 'grapheme' });
const bytes = new TextEncoder();

for (const s of ['USD 70.00', '予約', 'حجز', family]) {
  console.log(s, s.length, [...s].length, [...seg.segment(s)].length, bytes.encode(s).length);
}
// USD 70.00 9 9 9 9
// 予約 2 2 2 6
// حجز 3 3 3 6
// 👨‍👩‍👧 8 5 1 18

Cutting with .slice can split a surrogate pair. 'Thanks👍'.slice(0, 7) keeps half an emoji (the code unit \ud83d), and when it is saved as UTF-8 that half turns into U+FFFD, the replacement character � (bytes EF BF BD).

Normalisation: the twin é

One letter that looks the same can be written two ways. "é" can be one code point, U+00E9 (the NFC form), or "e" plus the accent mark U+0301 (the NFD form).

const a = '\u00E9';        // é, 1 code point
const b = 'e\u0301';       // e plus accent, 2 code points
a === b;                                   // false
a.length; b.length;                        // 1, 2
a.normalize('NFC') === b.normalize('NFC'); // true

A guest named "José" copied from two sources can fail to match, and search goes wrong too: 'José'.includes('Jose') gives false for the NFC form but true for the NFD form. Run normalize('NFC') when text comes in (before it's stored or compared), so all data uses the same form.

Mojibake: the right bytes, read the wrong way

Mojibake happens when UTF-8 bytes are read with another encoding. We turned text into UTF-8 bytes, then read them as windows-1252:

const bytes = new TextEncoder().encode('予約');      // E4 BA 88 E7 B4 84
new TextDecoder('windows-1252').decode(bytes);      // "予約"
new TextDecoder('utf-8').decode(bytes);             // "予約"

An empty box (tofu) is something else: the data is fine, but the font on that device has no picture (glyph) for that code point, say a new emoji on an old phone. A question mark ? usually means the text was once saved into an encoding that lacks the character, such as a database column that isn't UTF-8.

Declaring UTF-8 in the page, the server and the database

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">   <!-- the first line in <head> -->
  <title>Terrace Room · Mist Homestay</title>

⚠️ Why does text get cut or turn into strange letters?

TL;DR: Unicode gives every character one code point (U+4E88 for 予); Unicode 17.0 has 159,801 characters. UTF-8 writes a code point as 1 to 4 bytes: Latin 1, Arabic 2, Japanese 3, emoji 4. UTF-8 can store every character in the world while ASCII has only 128, and ASCII text is already valid UTF-8. In JavaScript, .length counts UTF-16 code units, [...s].length code points, Intl.Segmenter what you see, TextEncoder bytes: '予約' is 2, 2, 2 and 6 bytes; the family emoji 8, 5, 1 and 18 bytes. One letter can be NFC or NFD, so normalize('NFC') before comparing. Mojibake is UTF-8 bytes read with another encoding ("予約", "USDÂ"); tofu is a font with no glyph for it. Declare UTF-8 in <meta charset>, the Content-Type header, and a utf8mb4 database.

⚡ One number, 1 to 4 bytes

Meaning: Unicode gives every character one number, called a code point (U+4E88 for 予). UTF-8 writes that number as 1 to 4 bytes: a Latin letter 1, an Arabic letter 2, a Japanese character 3, an emoji 4. So UTF-8 can store every character in the world, while ASCII has only 128 characters and not a single Japanese one.

⚡ Three ways to measure length

Write:

for (const s of ['USD 70.00', '予約', 'حجز', family]) {
  console.log(s, s.length, [...s].length, [...seg.segment(s)].length, bytes.encode(s).length);
}
Guess first: the Japanese guest types 予約, two characters. What is '予約'.length, and how many bytes go to the server?

'予約'.length is 2, but 6 bytes go to the server. The family emoji is stranger still: .length 8, code points 5, what you see 1, and 18 bytes.

Meaning: .length counts UTF-16 code units, [...s].length code points, Intl.Segmenter what the eye sees, and TextEncoder UTF-8 bytes.

⚡ Mojibake: the right bytes, read the wrong way

Write:

const bytes = new TextEncoder().encode('予約');      // E4 BA 88 E7 B4 84
new TextDecoder('windows-1252').decode(bytes);      // "予約"

Meaning: The data isn't broken; its UTF-8 bytes are only read with another encoding. Declare UTF-8 in <meta charset="utf-8">, the Content-Type header and a utf8mb4 database.

What you will learn