Base64 shows up everywhere once you start noticing it: data URIs in CSS, JWT tokens, email attachments, embedded images, API payloads. Most developers use it long before they understand what it's really doing. That's fine until something breaks and you're staring at a string of gibberish wondering why your image won't render.
Here's the mental model that'll save you time.
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 /. The = character is used for padding.
It takes raw bytes and turns them into text that survives systems designed to handle text, not arbitrary binary. That's the whole point. It is not encryption, not compression, and not hashing. Anyone can decode it instantly. If you remember one thing, remember that: base64 is a transport format, not a security measure.
The mechanism is simple. It reads your data in groups of 3 bytes (24 bits), then splits those 24 bits into four 6-bit chunks. Each 6-bit chunk maps to one of the 64 characters. Three bytes in, four characters out — which is why base64 output is always about 33% larger than the input.
When you'd actually use it
Base64 earns its keep whenever binary data has to pass through a channel that expects text:
- Data URIs — embedding a small icon or font directly in HTML/CSS so you skip an extra HTTP request.
- Email attachments — SMTP was built for 7-bit ASCII, so binary files get base64-encoded (that's what MIME does under the hood).
- JSON and XML payloads — JSON has no binary type, so if you need to ship a thumbnail or a PDF inside a JSON field, base64 is the usual answer.
- Tokens and credentials — JWTs are base64url-encoded segments; HTTP Basic Auth encodes
user:passas base64. - Storing binary in text-only stores — config files, environment variables, some databases.
When should you not use it? When the channel already handles binary fine. Base64-encoding a large file just to POST it to an endpoint that accepts multipart/form-data wastes 33% bandwidth for nothing.
A quick worked example
Let's encode the word Hi! by hand to see the machinery.
The ASCII bytes are:
H = 72 = 01001000
i = 105 = 01101001
! = 33 = 00100001Concatenate the bits into one 24-bit block and re-split into four 6-bit groups:
01001000 01101001 00100001
→ 010010 000110 100100 100001
→ 18 6 36 33Map each number to the base64 alphabet (index 0 = A):
18 → S
6 → G
36 → k
33 → h
"Hi!" → "SGkh"Since Hi! is exactly 3 bytes, no padding is needed. If you encoded just Hi (2 bytes), you'd get SGk= with one padding character. One leftover byte produces two padding characters (==).
In practice you won't do this by hand. In most languages it's a one-liner:
// JavaScript (browser)
btoa("Hi!"); // "SGkh"
atob("SGkh"); // "Hi!"
// Node.js
Buffer.from("Hi!").toString("base64");
Buffer.from("SGkh", "base64").toString();
// Python
import base64
base64.b64encode(b"Hi!") # b'SGkh'
base64.b64decode("SGkh") # b'Hi!'For quick one-off checks — decoding a JWT payload, inspecting a data URI, or verifying what some service encoded — a browser tool beats spinning up a REPL. Our Base64 Encoder / Decoder handles both directions and runs right in the page.
The gotchas that bite people
Unicode and btoa
The browser's btoa() only works on strings where every character is in the Latin-1 range. Pass it an emoji or an accented character and it throws a InvalidCharacterError. The fix is to encode to UTF-8 bytes first:
// Safe UTF-8 base64 in the browser
function encode(str) {
return btoa(
String.fromCharCode(...new TextEncoder().encode(str))
);
}
encode("café"); // worksbase64 vs base64url
Standard base64 uses + and /. Those characters are unsafe in URLs and filenames — + becomes a space when URL-decoded, and / looks like a path separator. The base64url variant swaps them for - and _, and often drops the padding. JWTs use base64url. If you decode a token with a plain base64 decoder and it fails, this is usually why.
It's not security
I've seen base64 used to "hide" API keys in client-side code. It hides nothing. Anyone with dev tools decodes it in a second. If something needs to be secret, encrypt it or keep it server-side.
The size overhead
That 33% inflation matters at scale. Embedding a 2MB image as a data URI means shipping roughly 2.7MB of text, and it won't be cached separately. For large or frequently reused assets, a normal URL and HTTP caching wins. Base64 inlining is best for tiny assets.
Line wrapping
Some older formats (MIME, PEM certificates) wrap base64 at 64 or 76 characters per line. If a decoder chokes on your input, stray newlines are a common cause — strip whitespace before decoding, or make sure your decoder tolerates it.
A sensible default
Reach for base64 when you need binary data to ride safely through a text channel, and nowhere else. Keep assets as regular files when you can. Watch for the Unicode and base64url traps, and never mistake it for a privacy layer. Once the model clicks — bytes in, printable text out, 33% bigger, fully reversible — base64 stops being mysterious and becomes just another tool in the box.
Photo by Jake Walker on Unsplash