Base64 is one of those things you use constantly without thinking about it. It's in your JWTs, your data URIs, the Authorization headers you copy-paste, and every email attachment you've ever sent. But a surprising number of developers treat it like magic. It isn't. It's a simple, well-defined scheme, and understanding it saves you from some genuinely annoying bugs.
What Base64 actually is
Base64 is an encoding that represents binary data using only 64 printable ASCII characters: A-Z, a-z, 0-9, plus + and /. The = character is used for padding.
The mechanics are straightforward. You take your input bytes three at a time — that's 24 bits — and chop them into four 6-bit groups. Each 6-bit value (0–63) maps to one character in the Base64 alphabet. Three input bytes become four output characters. That's why Base64 always inflates your data by roughly 33%.
When the input length isn't a multiple of three, you pad. One leftover byte produces two characters plus ==; two leftover bytes produce three characters plus a single =.
It is not encryption
Let's get this out of the way, because people still get it wrong in production. Base64 is not encryption, hashing, or compression. Anyone can decode it instantly. If you Base64-encode a password and think you've secured it, you've secured nothing. It's purely a transport format — a way to shove binary through channels that only tolerate text.
When and why you'd use it
The core use case is simple: you have binary data and you need to move it through something that was designed for text.
- Data URIs — embedding a small image or font directly in CSS or HTML with
data:image/png;base64,...so the browser doesn't make a separate request. - JSON payloads — JSON has no native binary type, so file contents, cryptographic keys, or byte blobs get Base64-encoded before they go into a string field.
- Email (MIME) — the original motivation. SMTP was built for 7-bit ASCII, so attachments are Base64-encoded.
- HTTP Basic Auth —
username:passwordencoded to Base64 and dropped into the header. - JWTs — each segment is Base64url-encoded JSON.
The pattern is always the same: text-only pipe, binary cargo.
A short worked example
Say you want to encode the string Dev:ly for a Basic Auth header. Here's what happens with Node:
// Encode
const encoded = Buffer.from('Dev:ly').toString('base64');
console.log(encoded); // RGV2Omx5
// Decode
const decoded = Buffer.from('RGV2Omx5', 'base64').toString('utf8');
console.log(decoded); // Dev:lyIn the browser you'd use btoa and atob, though those come with a caveat we'll get to. For quick one-off checks — inspecting a JWT header, sanity-checking an encoded blob, or decoding something a colleague pasted in Slack — a browser tool like the Base64 Encoder / Decoder is faster than spinning up a REPL.
The gotchas that actually bite
1. Base64 vs Base64url
Standard Base64 uses + and /. Both are problematic in URLs and filenames — + becomes a space when form-decoded, and / is a path separator. That's why there's a URL-safe variant, Base64url, which swaps them for - and _ and often drops padding entirely.
JWTs use Base64url. If you try to decode a JWT segment with a plain Base64 decoder, it may choke on the -/_ characters or the missing padding. Always check which variant you're dealing with.
2. btoa/atob and Unicode
The browser's btoa only handles characters in the Latin-1 range. Feed it an emoji or a non-Latin character and it throws:
btoa('café'); // ❌ InvalidCharacterErrorThe fix is to encode to UTF-8 bytes first:
// Encode Unicode safely
const encoded = btoa(
String.fromCharCode(...new TextEncoder().encode('café'))
);
// Decode back
const bytes = Uint8Array.from(atob(encoded), c => c.charCodeAt(0));
const decoded = new TextDecoder().decode(bytes);This trips up people constantly, especially when a string looks fine in tests but breaks the moment a user types an accented name.
3. The 33% size tax
Base64 makes data bigger. For small inline assets that saves a request and is worth it. For large files you're wasting bandwidth and memory — a 10MB file becomes ~13.3MB and has to be fully decoded before use. Don't Base64 a video into a JSON field and wonder why your API is slow.
4. Line wrapping
MIME traditionally wraps Base64 at 76 characters with line breaks. Some decoders expect those newlines, others reject them. If you're parsing Base64 from an email or a PEM file, strip or preserve whitespace deliberately rather than assuming.
5. Padding strictness
Some decoders demand correct = padding; others happily ignore it. If you're generating Base64 that another system will consume, keep the padding unless the spec explicitly says to drop it. Mismatched expectations here produce errors that look mysterious until you count characters.
A mental model to keep
Think of Base64 as a reversible, transparent envelope. It doesn't protect anything, it doesn't shrink anything, and anyone can open it. Its only job is to make binary safe to carry through text. Once you hold that model in your head, the gotchas stop being surprises.
So: reach for it when you need binary in a text pipe. Use Base64url for anything touching URLs or JWTs. Encode to UTF-8 before btoa. And keep an eye on size when the payload gets large. Get those four things right and Base64 becomes exactly what it should be — boring, dependable plumbing.
Photo by Jake Walker on Unsplash