JavaScript Window atob() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Method

Definition

window.atob() decodes a Base64 string into a binary string (MDN). Pair it with btoa() to encode again.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call window.atob. Start with this line.

JavaScript
atob(encodedData)
CallWhat it does
atob(encodedData)Primary signature.
window.atob(...)Call with the arguments your case needs.

Built into the browser. Call window.atob(...) (or the global atob(...)) β€” no import required.

Library declarations
JavaScript
atob(encodedData)

Parameters

ParameterDescription
encodedDataRequired. Base64 string from btoa() alphabet

Return Value

Return ValueDescription
*Binary string (bytes as chars)

JavaScript Examples

Example 1 — Basic Decode

Decode a Base64 string produced by btoa.

JavaScript
const encoded = btoa("Hello, world");
const decoded = atob(encoded);
console.log(decoded); // Hello, world
Try It Yourself

How It Works

  1. Encode first. btoa makes valid Base64.
  2. Call atob. Pass that Base64 string.
  3. Read the result. ASCII text comes back readable.
  4. Tip. Same as window.atob(...) in browsers.

Example 2 — Round-Trip

Prove encode then decode restores the original ASCII string.

JavaScript
const original = "CodeToFun";
const back = atob(btoa(original));
console.log(back === original); // true
console.log(back);              // CodeToFun
Try It Yourself

How It Works

  1. Start with ASCII. Safe for btoa/atob.
  2. Encode then decode. Nested atob(btoa(...)).
  3. Compare. Strings match for Latin-1-safe text.
  4. Unicode warning. Emoji / non-Latin-1 needs a different approach.

Example 3 — Known Base64

Decode a fixed Base64 literal without calling btoa first.

JavaScript
const decoded = atob("SGVsbG8=");
console.log(decoded); // Hello
Try It Yourself

How It Works

  1. Use a valid payload. SGVsbG8= is Base64 for Hello.
  2. Call atob. No encode step needed.
  3. Padding matters. Trailing = is part of Base64.
  4. Tip. Useful when Base64 comes from an API or file.

Example 4 — Invalid Input

Bad Base64 throws InvalidCharacterError β€” catch it.

JavaScript
try {
  atob("not%%%valid");
} catch (err) {
  console.log(err.name); // InvalidCharacterError
}
Try It Yourself

How It Works

  1. Pass bad data. Characters outside the Base64 alphabet.
  2. Browser throws. DOMException named InvalidCharacterError.
  3. Catch it. Avoid crashing the page.
  4. Validate first when input comes from users or the network.

Example 5 — data: URL Payload

Strip the prefix, then decode only the Base64 part.

JavaScript
const dataUrl = "data:text/plain;base64,SGVsbG8=";
const payload = dataUrl.split(",")[1];
console.log(atob(payload)); // Hello
Try It Yourself

How It Works

  1. Do not decode the whole URL. The data:... prefix is not Base64.
  2. Split on ,. Take index 1.
  3. Call atob. Decode the payload only.
  4. Same idea for many data:image/... URLs (binary result).

Browser Support

Window.atob() is marked Baseline Widely available on MDN (since July 2015). Browser Window API β€” not a Node.js built-in.

✓ Baseline · Widely available

atob()

Decode Base64 strings across modern browsers.

99% Core tag support
Chrome Yes
Full support
Edge Yes
Full support
Firefox Yes
Full support
Safari Yes
Full support
Opera Yes
Full support
HTML element 100% supported

Bottom line: Safe for production ASCII / binary-string Base64. Prefer byte APIs when you need typed arrays.

Frequently Asked Questions

It decodes a Base64-encoded string into a binary string (each character is one byte, code point 0–255). For ASCII text encoded with btoa(), the result is readable plain text.
No. MDN marks Window.atob() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
btoa() encodes a binary string to Base64; atob() decodes Base64 back to a binary string. They are inverse operations for Latin-1 / byte-safe data.
The input is not valid Base64β€”wrong characters, bad padding, or corrupted data. Wrap calls in try/catch or validate input first.
No. Strip the prefix first (everything before the comma), then pass only the Base64 payload.
Not directly for arbitrary Unicode. atob/btoa work on binary strings, not UTF-8 text. For Unicode, encode/decode bytes (for example with TextEncoder) or use a dedicated Base64 helper.

Next: blur()

Remove focus from the browsing context / window.

blur() →

About the author

Mari Selvan M P
Mari Selvan M P πŸ”—

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful