Base64 is one of those things you use constantly without really thinking about it. It's in your JWT tokens, your inline images, your email attachments, your Basic Auth headers. Most developers know roughly what it does but not why it exists or when it's the wrong tool. Let's fix that.
What Base64 actually is
Base64 is a binary-to-text encoding. It takes arbitrary bytes and represents them using only 64 printable ASCII characters: A-Z, a-z, 0-9, plus + and /, with = used for padding.
The mechanics are simple. It chews through your input three bytes (24 bits) at a time, then splits those 24 bits into four 6-bit groups. Each 6-bit value (0–63) maps to one character in the alphabet. Three input bytes become four output characters, which is why Base64 output is always about 33% larger than the original.
That size increase matters. Base64 is not compression, and it's definitely not encryption. It's a way to move binary data through channels that only reliably handle text.
When you'd actually reach for it
The common thread is always the same: something in the pipeline can't be trusted with raw bytes.
- Data URIs — embedding a small image or font directly in CSS or HTML with
data:image/png;base64,...to avoid an extra HTTP request. - JSON and XML payloads — JSON has no native binary type, so a file upload inside a JSON body gets Base64-encoded.
- Email (MIME) — SMTP was built for 7-bit ASCII, so attachments get encoded to survive the trip.
- HTTP Basic Auth — the
Authorization: Basicheader is justbase64(username:password). - JWTs — the header and payload are Base64URL-encoded JSON (more on that variant below).
When NOT to use it
Don't Base64-encode large files just to shove them into a JSON API if you can use multipart/form-data instead — you're paying a 33% bandwidth tax for nothing. And never treat Base64 as a security measure. Anyone can decode it in one line. If something is secret, encrypt it.
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 33Map each value to the Base64 alphabet (index 0 is A): 18=S, 6=G, 36=k, 33=h. So Hi! becomes SGkh.
In code, you rarely do this by hand. Here's how it looks across a few environments:
// JavaScript (browser)
btoa("Hi!"); // "SGkh"
atob("SGkh"); // "Hi!"
// Node.js (handles binary properly)
Buffer.from("Hi!").toString("base64"); // "SGkh"
Buffer.from("SGkh", "base64").toString(); // "Hi!"
# Python
import base64
base64.b64encode(b"Hi!") # b'SGkh'
base64.b64decode(b"SGkh") # b'Hi!'When you just need to check a value, inspect a JWT payload, or sanity-test an encoding bug without opening a REPL, a browser tool is quicker. Our Base64 Encoder / Decoder handles both directions and runs entirely in your browser, so you're not pasting sensitive strings into some random server.
The gotchas that actually bite people
1. btoa and Unicode don't mix
The classic JavaScript trap: btoa only works on characters in the Latin-1 range. Hand it an emoji or a non-Latin character and it throws:
btoa("café"); // throws: InvalidCharacterErrorThe fix is to encode to UTF-8 bytes first. In modern browsers:
function encodeUtf8(str) {
return btoa(String.fromCharCode(...new TextEncoder().encode(str)));
}
encodeUtf8("café"); // worksNode's Buffer doesn't have this problem because it deals in bytes from the start.
2. Standard vs URL-safe Base64
Standard Base64 uses + and /, which both have special meaning in URLs and filenames. Base64URL swaps them for - and _ and usually drops the = padding. JWTs use this variant. If you decode a JWT with a plain Base64 decoder and it fails, this is almost always why. Translate the characters back before decoding, or use a decoder that understands the URL-safe alphabet.
3. Padding and whitespace
Some decoders are strict about the trailing = padding; others don't care. And many real-world Base64 strings (especially from PEM certificates or email) contain line breaks every 64 or 76 characters. A strict decoder will choke on those newlines, so strip whitespace before decoding if you're not sure.
4. It's not a checksum or an ID generator
Base64 is reversible and deterministic. Encoding a user's email to use as a "token" offers zero protection — it's trivially decoded. People do this surprisingly often. Don't.
A mental model that sticks
Think of Base64 as bubble wrap for bytes. It makes your data bigger and completely visible, but it protects it from being mangled by systems that only speak text. You unwrap it the moment it arrives somewhere binary-friendly.
Keep three rules in your head and you'll avoid most trouble:
- Encode to UTF-8 bytes first if you're dealing with text that isn't plain ASCII.
- Know whether you need standard or URL-safe output before you start.
- Never confuse encoding with security — if it needs to stay private, encrypt it.
That's the whole story. Base64 is simple once you see the 3-bytes-to-4-characters pattern underneath, and the gotchas are predictable. Next time a JWT won't decode or btoa throws on an accented name, you'll know exactly where to look.
Photo by Jake Walker on Unsplash