Sub topic 7
btoa and atob, and Why They Reject Unicode
This chapter assumes you know how base64 maps 3 bytes to 4 characters (previous chapter), that JavaScript strings are sequences of UTF-16 code units, and that UTF-8 encodes non-ASCII characters as multiple bytes.
The working model: strings that pretend to be bytes
btoa ("binary to ASCII") and atob ("ASCII to binary") predate typed arrays. When they were designed, JavaScript had no byte container, so they use a binary string as one. A binary string is an ordinary string where every character has a code unit between 0x00 and 0xFF, and each character stands for one byte.
btoa(binaryString)reads each character's code unit as a byte value and returns the base64 text.atob(base64)decodes the base64 and returns a binary string with one character per byte.
Neither function knows anything about UTF-8. Both treat characters as bytes, one to one.
Failure mode 1: characters above U+00FF throw
A character like € (U+20AC) cannot fit in a single byte, so btoa refuses it:
try {
btoa("€");
} catch (err) {
console.log(err.name); // "InvalidCharacterError"
}
console.log(btoa("hi")); // "aGk="
console.log(atob("aGk=")); // "hi"The error is a DOMException named InvalidCharacterError. Emoji, CJK text, Cyrillic, and most symbols all trigger it.
Failure mode 2: Latin-1 characters "work" but produce the wrong bytes
This failure is quieter. é is U+00E9, which is at or below 0xFF, so btoa accepts it and encodes the single byte 0xE9. In UTF-8, é is two bytes, 0xC3 0xA9.
console.log(btoa("é")); // "6Q==" (byte E9, Latin-1)
// The UTF-8 bytes of "é" encode differently:
const utf8 = new TextEncoder().encode("é");
console.log(utf8); // Uint8Array [195, 169]The UTF-8 base64 for é is "w6k=". A server that decodes "6Q==" as UTF-8 gets the invalid byte 0xE9 and shows a replacement character. Your tests pass with "hello" and fail with the first French customer name.
Failure mode 3: atob output is bytes, not text
atob returns a binary string, so decoding base64 that holds UTF-8 gives you one character per byte. That is mojibake: text decoded with the wrong encoding.
const result = atob("4oKs"); // base64 of the UTF-8 bytes for "€"
console.log(result.length); // 3
console.log([...result].map(c => c.charCodeAt(0).toString(16)));
// ["e2", "82", "ac"] (three bytes, not one "€")The bytes are correct, but they are still bytes. To get "€" back, you have to move them into a Uint8Array and run them through TextDecoder. The next chapter shows how.
What atob accepts and rejects
| Input to `atob` | Result |
|---|---|
| `"aGk="` | `"hi"` |
| `"aGk"` (missing padding) | `"hi"`. Padding is optional |
| `"aG k="` (contains a space) | `"hi"`. ASCII whitespace is stripped |
| `"a-b_"` (base64url characters) | Throws `InvalidCharacterError` |
| `"a"` (length 1 mod 4) | Throws `InvalidCharacterError` |
The base64url row matters for tokens. atob only accepts the standard alphabet with + and /.
The legacy workaround
Older code often contains btoa(unescape(encodeURIComponent(text))). It works because encodeURIComponent produces UTF-8 percent escapes and unescape turns each escape into a one-byte character. However, unescape is deprecated, and this approach is hard to read and to apply to raw binary data. A clearer route goes through TextEncoder and Uint8Array.
Exercise
A colleague runs btoa(name) on user names. "Zoë" encodes without error, but the backend, which decodes base64 as UTF-8, shows "Zo\uFFFD". "李" throws. Explain both outcomes.
Show answer
"ë" is U+00EB, which is at or below 0xFF, so btoa accepts it and encodes it as the single Latin-1 byte 0xEB. On its own, 0xEB is not valid UTF-8, so the backend's decoder substitutes U+FFFD. "李" is U+674E, which is above 0xFF, so btoa throws InvalidCharacterError. Both problems go away if the name is encoded to UTF-8 bytes first and those bytes are passed to btoa.
Exercise
What does atob("4oKs").length return, and why is it not 1?
Show answer
It returns 3. atob returns a binary string with one character per decoded byte, and "€" is three bytes in UTF-8 (E2 82 AC). atob does not interpret those bytes as UTF-8.