Short answer: in the browser, atob() decodes Base64 to a string — atob("SGVsbG8=") gives you "Hello". In Node.js, use Buffer.from(b64, "base64").toString("utf8"). The one-liner breaks in three common situations: non-ASCII text comes back as mojibake, missing = padding throws, and base64url strings (the kind inside JWTs and URLs) use different characters. Handle those three and decoding works everywhere.
The one-liner
// Browser
atob("SGVsbG8sIHdvcmxkIQ=="); // "Hello, world!"
// Node.js
Buffer.from("SGVsbG8sIHdvcmxkIQ==", "base64").toString("utf8"); // "Hello, world!"
// Python
import base64
base64.b64decode("SGVsbG8sIHdvcmxkIQ==").decode("utf-8") # "Hello, world!"The Unicode trap
atob() returns a string where each character is one byte — Latin-1, not UTF-8. Decode Base64 that holds non-ASCII text and you get mojibake: the checkmark ✓ encodes to "4pyT", and atob("4pyT") returns the three garbage characters "✓" instead of "✓". The fix is to treat the result as raw bytes and decode them as UTF-8:
function b64decodeUnicode(b64) {
const bytes = Uint8Array.from(atob(b64), (c) => c.charCodeAt(0));
return new TextDecoder().decode(bytes);
}
b64decodeUnicode("4pyT"); // "✓" — correctThis is the same trap in reverse when encoding: btoa() throws InvalidCharacterError on any character above U+00FF. Node's Buffer and Python's base64 handle Unicode correctly out of the box — only the browser one-liner needs the TextDecoder wrapper.
Padding and base64url
Standard Base64 output is always a multiple of 4 characters, padded with = at the end. Two variants break the naive decoder. First, many producers strip the padding (JWTs do this) — atob() then throws because the length is not a multiple of 4. Re-add it: b64 += "=".repeat((4 - (b64.length % 4)) % 4). Second, base64url swaps + and / for - and _ so the string survives inside URLs and filenames. Convert back before decoding:
function b64urlDecode(b64url) {
let b64 = b64url.replace(/-/g, "+").replace(/_/g, "/");
b64 += "=".repeat((4 - (b64.length % 4)) % 4); // restore padding
return new TextDecoder().decode(
Uint8Array.from(atob(b64), (c) => c.charCodeAt(0))
);
}Decoding images and files
Base64 images usually arrive as data URIs — data:image/png;base64,iVBORw0KGgo... The part after the comma is the payload; the part before it names the MIME type. Strip the prefix, decode to bytes, and wrap them in a Blob to get a real file back:
function dataUriToBlob(dataUri) {
const [header, b64] = dataUri.split(",");
const mime = header.match(/data:(.*?);/)[1];
const bytes = Uint8Array.from(atob(b64), (c) => c.charCodeAt(0));
return new Blob([bytes], { type: mime });
}
// Show it: img.src = URL.createObjectURL(dataUriToBlob(uri));
// Save it: a.href = URL.createObjectURL(blob); a.download = "image.png";The errors you will actually see
- InvalidCharacterError (browser): the string contains a character outside the Base64 alphabet — often whitespace, line breaks, or a stray quote copied from JSON. Strip everything that is not A–Z a–z 0–9 + / = (or - _ for base64url) before decoding.
- Incorrect padding (Python): the = padding is missing or the length is not a multiple of 4. Add padding with b64 += "=" * (-len(b64) % 4).
- Mojibake instead of an error: you decoded UTF-8 bytes as Latin-1. Use the TextDecoder version above.
- Decoded output that is still unreadable: the payload may be gzipped or it may be double-encoded. Try decoding twice — if the second pass produces clean text, the producer encoded an already-encoded string.
Quick answers
- Is Base64 encryption? No. It is an encoding — anyone can decode it. Never treat Base64 as a way to hide secrets; it only makes binary data survive text channels.
- How do I decode a huge Base64 string? atob() handles megabytes fine in practice, but for very large payloads decode in chunks or stream it — building one giant intermediate string is what runs out of memory, not the decoding itself.
- Why does my Base64 have no = at the end? The producer used unpadded base64url (common in JWTs and URL tokens). Re-add the padding as shown above.
Try it
Paste any Base64 string into the URL & Base64 Encoder — it decodes text in your browser and flags the padding and character errors above before they bite. Going the other direction? How to Encode Text to Base64 covers encoding, including the same Unicode trap from the other side.