Blog ·
Base64 Encode Unicode in JavaScript: Emoji and UTF-8 Fix
btoa() throws on emoji and non-Latin-1 text. Encode UTF-8 bytes with TextEncoder first, then btoa(). Includes TextDecoder decoding and Node.js Buffer.
Base64 Encoder / Decoder
Encode and decode Base64 instantly in your browser.
To base64 encode Unicode text in JavaScript, convert the string to UTF-8 bytes with TextEncoder, turn those bytes into a binary string, and pass that string to btoa(). Calling btoa() on the original string throws for any character above U+00FF, which includes emoji and most non-Latin scripts. To check an encoding without writing code, the Base64 Encoder / Decoder handles full UTF-8 input.
Why btoa() throws on emoji
btoa() takes a string and writes each character as one byte. It only accepts characters in the Latin-1 range, code points 0 to 255. Anything larger is rejected.
btoa('hello')
// 'aGVsbG8='
btoa('héllo')
// 'aOlsbG8=' (é is U+00E9, which fits in one Latin-1 byte, E9)
btoa('😀')
// Typical browser console output (wording varies by browser):
// Uncaught DOMException: Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.The error happens because the emoji 😀 is U+1F600, far above 255. It has no single-byte form, so btoa() has nothing to encode. Node.js throws a DOMException for the same call, but with different wording, so match on the error type rather than the message.
The fix is to choose a byte representation first. Base64 encodes bytes, not characters. For text, the usual choice is UTF-8, which most APIs, files and protocols expect.
Encode Unicode text to Base64 with TextEncoder
TextEncoder is available in modern browsers and in Node.js. It converts a string to a Uint8Array of UTF-8 bytes. You then build a binary string where each character is one byte, and call btoa() on it.
function base64EncodeUnicode(str) {
const bytes = new TextEncoder().encode(str);
let binary = '';
for (const byte of bytes) {
binary += String.fromCharCode(byte);
}
return btoa(binary);
}
base64EncodeUnicode('😀')
// '8J+YgA=='
base64EncodeUnicode('héllo')
// 'aMOpbGxv'The emoji 😀 becomes the four UTF-8 bytes F0 9F 98 80, which Base64 encodes as 8J+YgA==. This matches the Node.js output shown later, so the result is the same across platforms.
The plain btoa() call and the wrapper give different results for 'héllo'. btoa() returns 'aOlsbG8=' because it writes é as the single Latin-1 byte E9. The wrapper returns 'aMOpbGxv' because it writes é as the UTF-8 bytes C3 A9. Most systems expect the UTF-8 form when the input is text.
Decode Base64 back to Unicode text with TextDecoder
Decoding reverses the steps. atob() returns a binary string with one character per byte. You copy those byte values into a Uint8Array and let TextDecoder read them as UTF-8.
function base64DecodeUnicode(b64) {
const binary = atob(b64);
const bytes = Uint8Array.from(binary, (c) => c.charCodeAt(0));
return new TextDecoder().decode(bytes);
}
base64DecodeUnicode('8J+YgA==')
// '😀'
base64DecodeUnicode('aMOpbGxv')
// 'héllo'If you call atob() alone on '8J+YgA==', you get four characters: U+00F0, U+009F, U+0098 and U+0080. The first one, ð, is visible. The other three are control characters that most consoles show as nothing or as odd symbols. Those four characters are the raw UTF-8 bytes read as Latin-1. Passing the bytes through TextDecoder restores the emoji.
Common pitfalls
- Skipping the decode step. If you use atob() on data that was encoded from UTF-8, you get mojibake, not an error. When the source is text, always decode the bytes as UTF-8.
- Using encodeURIComponent() as a shortcut. It produces percent-encoding for URLs, not Base64. If you need Base64 text, encode the bytes instead.
- Assuming string length equals byte length. '😀'.length is 2 in JavaScript because it is a surrogate pair, but its UTF-8 form is 4 bytes. Size calculations should use the encoded bytes.
- Mixing up standard Base64 and Base64URL. The code above produces standard Base64, with + and / and = padding. For URL-safe output, see Base64 vs Base64URL.
Node.js: Buffer does the UTF-8 step for you
In Node.js, Buffer converts strings to bytes directly. Pass 'utf8' explicitly so the encoding is clear to the next reader.
Buffer.from('😀', 'utf8').toString('base64')
// '8J+YgA=='
Buffer.from('8J+YgA==', 'base64').toString('utf8')
// '😀'Buffer is Node-only. Browsers do not provide it by default, so use the TextEncoder and TextDecoder approach in front-end code.
Python for comparison
Python's base64 module works on bytes, so encode the string to UTF-8 first. Emoji need no special handling.
import base64
base64.b64encode('😀'.encode('utf-8')).decode('ascii')
# '8J+YgA=='
base64.b64decode('8J+YgA==').decode('utf-8')
# '😀'Test with real data carefully
Base64 is encoding, not encryption. Anyone who has the encoded text can decode it. If you are testing with tokens, passwords or other secrets, avoid pasting them into any online tool unless you have checked how that tool handles input. For secrets, local code is the safest choice.
FAQ
Where exactly does btoa() stop accepting characters?
btoa() accepts characters with code points up to 255 (U+00FF), the Latin-1 range. That includes letters such as é and ñ. Characters above U+00FF, such as € (U+20AC) or any emoji, cause an error. The TextEncoder method works for all of them.
Is the output of the TextEncoder method standard Base64?
Yes. It uses the standard Base64 alphabet with = padding. Only the input bytes differ from a naive approach, and those bytes are the UTF-8 encoding that most systems use.
Can I use the same code for large strings?
The loop above is fine for typical payloads. For very large inputs, consider processing in chunks or using a streaming API, because building a long binary string one character at a time can be slow.
How do I know the Base64 I received was UTF-8?
Usually you do not, unless the protocol or documentation says so. If decoding produces sequences like é instead of é, the data was probably UTF-8 bytes read as Latin-1. Check the producer's documentation when you can.
Base64 Encoder / Decoder
Encode and decode Base64 instantly in your browser.