Base64 is one of those things you use constantly without thinking about it. It's in your JWTs, your email attachments, your data URIs, your API credentials. And yet it's also one of the most misunderstood tools in the kit — people reach for it expecting it to do things it was never designed to do.
So let's clear it up.
What Base64 actually is
Base64 is an encoding scheme that represents binary data using only 64 printable ASCII characters: A–Z, a–z, 0–9, plus + and /, with = used for padding. That's it. It takes arbitrary bytes and turns them into text that survives channels which only reliably handle plain ASCII.
The mechanics are simple: it chops your input into 6-bit groups (since 2^6 = 64) and maps each group to one of those 64 characters. Three bytes (24 bits) become four Base64 characters. Because of that 3-to-4 ratio, encoded output is always about 33% larger than the input. Keep that number in your head — it matters more than you'd think.
The one thing to be absolutely clear about: Base64 is not encryption, and it is not compression. It offers zero security. Anyone can decode it instantly. If you're Base64-encoding a password and feeling clever, stop.
When you'd actually use it
Base64 earns its keep whenever binary data needs to travel through a text-only medium. Common cases:
- Data URIs — embedding small images or fonts directly in CSS/HTML so there's no extra HTTP request.
- Email attachments — MIME uses Base64 because SMTP was historically a 7-bit protocol.
- Storing binary in JSON or XML — JSON has no native binary type, so a PNG thumbnail goes in as a Base64 string.
- Basic Auth headers —
Authorization: Basicis just Base64 ofuser:password(which is exactly why Basic Auth needs HTTPS). - JWTs — each segment is Base64url-encoded JSON.
The theme: it's about transport safety, not secrecy. Use it when bytes might otherwise get mangled by something that expects text.
A quick worked example
Say you want to encode the string Hi!. Those three characters are the bytes 72 105 33, or in binary:
H i !
01001000 01101001 00100001Regroup those 24 bits into four 6-bit chunks:
010010 000110 100100 100001
18 6 36 33
S G k hWhich gives you SGkh. No padding needed here because the input was exactly three bytes. If you'd encoded just Hi (two bytes, 16 bits), you'd get SGk= — the trailing = pads the output to a multiple of four characters.
In Node it's a one-liner either direction:
// encode
Buffer.from('Hi!').toString('base64'); // 'SGkh'
// decode
Buffer.from('SGkh', 'base64').toString(); // 'Hi!'In the browser, btoa() and atob() do the job — but with a nasty caveat we'll get to in a second.
For quick one-off conversions while debugging, I usually just paste into a Base64 Encoder / Decoder rather than spinning up a REPL. Handy for inspecting a JWT payload or sanity-checking what some API shoved into a field.
The gotchas that bite people
1. btoa/atob choke on Unicode
The browser's btoa() only handles characters in the Latin-1 range. Pass it an emoji or a Cyrillic string and it throws InvalidCharacterError. You have to encode to UTF-8 bytes first:
// Safe Unicode-aware encoding in the browser
const encode = (str) =>
btoa(String.fromCharCode(...new TextEncoder().encode(str)));
const decode = (b64) =>
new TextDecoder().decode(
Uint8Array.from(atob(b64), c => c.charCodeAt(0))
);This trips up people constantly. If your app handles any non-ASCII text, don't trust raw btoa.
2. Base64 vs Base64url
Standard Base64 uses + and /. Those characters are unsafe in URLs and filenames. Base64url swaps them for - and _ and usually drops the padding. JWTs use this variant. If you decode a JWT segment with a standard decoder and it fails, this is often why — you may need to swap the characters back and re-pad before decoding.
3. The size tax
That 33% overhead is real. Inlining a large image as a data URI to save one request can backfire: the bloated Base64 string can't be cached separately, it blocks parsing while it loads, and it's bigger on the wire. Rule of thumb: inline tiny assets (icons, a 1px spacer), serve anything substantial as a normal file.
4. Whitespace and line breaks
Some encoders (classic MIME, OpenSSL) insert newlines every 64 or 76 characters. Many decoders tolerate that; some don't. If decoding mysteriously fails, strip whitespace first.
5. It's still not security
Worth repeating because people keep doing it. Obscuring a token or config value in Base64 does nothing. It's a speed bump a five-year-old with a decoder could clear.
A sensible mental model
Think of Base64 as a wrapper, not a vault. Its whole job is to make binary data safe to pass through text-based systems, and it does that reliably and reversibly. Any time you catch yourself expecting more from it — privacy, size savings, integrity checks — reach for the right tool instead: encryption for secrecy, gzip for size, a hash or signature for integrity.
Get those boundaries right and Base64 becomes exactly what it should be: boring, predictable plumbing you never have to think twice about.
Photo by Jake Walker on Unsplash