JavaScript · 8 min read

Base64 decode in JavaScript.

Browsers have had atob() for decades, and it still trips people up: it returns a “binary string”, not text. Here is how to decode Base64 correctly in the browser and in Node.js, including UTF-8, URL-safe input and files.

Updated 9 November 2026

Quick answer

In the browser, atob(s) decodes Base64, but it returns one character per byte, so anything outside ASCII comes out garbled. For text, turn the result into bytes and use TextDecoder: new TextDecoder().decode(Uint8Array.from(atob(s), c => c.charCodeAt(0))). In Node.js, use Buffer.from(s, "base64").toString("utf8"), or "base64url" for URL-safe input.

atob() and why it garbles UTF-8

atob("SGVsbG8sIHdvcmxkIQ==");   // "Hello, world!"
atob("Y2Fmw6kg4piV");           // "café â" + two invisible characters; expected "café ☕"

atob predates Unicode in JavaScript. It maps each decoded byte to the character with that code (0 to 255). That is fine for ASCII, but UTF-8 uses several bytes for characters such as é or ☕, and atob turns each of those bytes into its own Latin-1 character. The bytes are correct; only the interpretation is wrong.

The fix is to treat the result as bytes and decode them as UTF-8:

function b64ToText(b64) {
  const bin = atob(b64);
  const bytes = Uint8Array.from(bin, c => c.charCodeAt(0));
  return new TextDecoder().decode(bytes);   // UTF-8 by default
}

b64ToText("Y2Fmw6kg4piV");   // "café ☕"

Avoid the old decodeURIComponent(escape(atob(s))) trick you will find in older answers. escape() is deprecated, and the trick throws on invalid UTF-8 instead of giving you a choice.

See this example decoded →

What atob accepts and rejects

Browsers follow the WHATWG “forgiving-base64” rules, so atob is more lenient than many libraries:

Inputatob result
Missing padding: SGVsbG8Works: "Hello"
Spaces or newlines insideIgnored
URL-safe characters: _-7dThrows InvalidCharacterError
Impossible length: AThrows InvalidCharacterError

So for URL-safe strings (JWTs, OAuth tokens) swap the two characters back first:

const std = s.replace(/-/g, "+").replace(/_/g, "/");
const bytes = Uint8Array.from(atob(std), c => c.charCodeAt(0));

Wrap atob in try/catch when the input comes from a user. The error is a DOMException named InvalidCharacterError.

The modern way: Uint8Array.fromBase64()

Newer engines have a built-in that returns bytes directly and handles both alphabets. MDN lists it as Baseline since September 2025, so older browsers and current Node LTS releases may not have it. Feature-detect and fall back:

function b64ToBytes(s, urlSafe = false) {
  if (typeof Uint8Array.fromBase64 === "function") {
    return Uint8Array.fromBase64(s, { alphabet: urlSafe ? "base64url" : "base64" });
  }
  if (urlSafe) s = s.replace(/-/g, "+").replace(/_/g, "/");
  return Uint8Array.from(atob(s), c => c.charCodeAt(0));
}

Node.js: Buffer

In Node, Buffer is the usual tool. It decodes straight to bytes and can convert to text in one step:

Buffer.from("Y2Fmw6kg4piV", "base64").toString("utf8");          // "café ☕"
Buffer.from("eyJhbGciOiJIUzI1NiJ9", "base64url").toString("utf8"); // '{"alg":"HS256"}'

Two things to know. First, Buffer accepts both alphabets with either encoding name, and it accepts missing padding. Second, it never throws on bad input. Invalid characters are skipped, so Buffer.from("SGVsbG8!!", "base64") quietly returns Hello. If you need to reject malformed input, check it with a regular expression first:

const isB64 = s => /^[A-Za-z0-9+/]*={0,2}$/.test(s) && s.length % 4 !== 1;

Node also has atob as a global (since v16) for code shared with the browser, but Node's documentation calls it legacy and recommends Buffer.

Data URIs

A data URI is Base64 with a header: data:image/png;base64,iVBORw0KGgo.... Strip the header before calling atob, or let the browser do the whole job with fetch, which decodes data URIs natively:

// Manual
const b64 = dataUri.slice(dataUri.indexOf(",") + 1);

// Or let fetch decode it into a Blob (keeps the MIME type)
const blob = await (await fetch(dataUri)).blob();

Decode Base64 to a file

In the browser, wrap the bytes in a Blob and click a temporary download link:

function downloadB64(b64, filename, mime = "application/octet-stream") {
  const bytes = Uint8Array.from(atob(b64), c => c.charCodeAt(0));
  const url = URL.createObjectURL(new Blob([bytes], { type: mime }));
  const a = Object.assign(document.createElement("a"), { href: url, download: filename });
  a.click();
  URL.revokeObjectURL(url);
}

In Node, write the buffer directly:

const fs = require("fs");
fs.writeFileSync("output.pdf", Buffer.from(b64, "base64"));

To see what kind of file you have before saving it, check the first characters against the file-signature table, or paste the string into Base64 to Image or Base64 to PDF.

Check your output

Not sure the decode worked? Paste the original string into the decoder. It shows the text, the hex bytes and the detected file type side by side, so you can compare it with what your code produced. Nothing is uploaded; it runs in your browser.


Published 9 November 2026. Spot an error? Email contactus@base64decode.tools.

Frequently asked

Common questions.

atob returns one character per decoded byte. UTF-8 text uses several bytes for non-ASCII characters, so each byte shows up as a separate Latin-1 character. Convert the result to a Uint8Array and decode it with new TextDecoder().

In the browser, replace - with + and _ with / and then call atob. In Node.js, use Buffer.from(s, "base64url"). Where Uint8Array.fromBase64 is available, pass { alphabet: "base64url" }.

No. Browsers use the forgiving-base64 rules, so atob accepts strings without trailing = signs. It rejects strings whose length leaves one character over, and anything containing - or _.

Use Buffer.from(s, "base64"). Call .toString("utf8") for text, or write the Buffer to a file with fs.writeFileSync for binary data. Note that Buffer skips invalid characters instead of throwing.

atob is a web API that returns a binary string and throws on invalid input. Buffer.from is Node-specific, returns bytes, accepts both Base64 alphabets and silently ignores invalid characters.

Keep going

Related guides and tools.