JavaScript Window btoa() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Method

Definition

window.btoa() creates a Base64-encoded ASCII string from a binary string (MDN). Pair it with atob() to decode again.

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

Syntax

Call window.btoa. Start with this line.

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

Built into the browser. Call window.btoa(...) (or the global btoa(...)) — no import required.

Library declarations
JavaScript
btoa(stringToEncode)

Parameters

ParameterDescription
stringToEncodeRequired. Binary string (code points < 256)

Return Value

Return ValueDescription
*Base64 ASCII string

JavaScript Examples

Example 1 — Basic Encode

Encode a short ASCII string to Base64.

JavaScript
const encoded = btoa("Hello, world");
console.log(encoded); // "SGVsbG8sIHdvcmxk"
// same as: window.btoa("Hello, world")
Try It Yourself

How It Works

  1. Pass a string. Here every character is ASCII.
  2. Call btoa. Bytes are turned into Base64 text.
  3. Log the result. You get SGVsbG8sIHdvcmxk.
  4. Shorthand. btoa and window.btoa match in browsers.

Example 2 — Round-Trip with atob

Encode, then decode, to prove the pair is reversible for ASCII.

JavaScript
const original = "CodeToFun";
const encoded = btoa(original);
const decoded = atob(encoded);

console.log(encoded); // "Q29kZVRvRnVu"
console.log(decoded); // "CodeToFun"
console.log(decoded === original); // true
Try It Yourself

How It Works

  1. Store plain text. original is ASCII-safe.
  2. Encode with btoa. You get Base64.
  3. Decode with atob. You get the same characters back.
  4. Compare. === is true for this Latin-1 / ASCII case.

Example 3 — Single-Byte Character

MDN-style check: a Latin letter fits in one byte and encodes cleanly.

JavaScript
const ok = "a";
console.log(ok.codePointAt(0).toString(16)); // "61"
console.log(btoa(ok)); // "YQ=="
Try It Yourself

How It Works

  1. Pick one character. "a" is a single byte.
  2. Inspect the code point. Hex 61 is below FF.
  3. Encode. btoa("a") returns YQ==.
  4. Rule. Code points above 255 are not allowed as raw input.

Example 4 — Unicode Throws

Characters above one byte cause InvalidCharacterError.

JavaScript
const notOK = "✓";
console.log(notOK.codePointAt(0).toString(16)); // "2713"

try {
  console.log(btoa(notOK));
} catch (err) {
  console.log(err.name); // "InvalidCharacterError"
}
Try It Yourself

How It Works

  1. Use a multi-byte character. ✓ is not a single byte.
  2. Check the code point. Hex 2713 is greater than FF.
  3. Call btoa. The browser throws.
  4. Catch it. Handle InvalidCharacterError or convert to UTF-8 first.

Example 5 — Unicode via UTF-8 Bytes

MDN-style path: TextEncoder → binary string → btoa.

JavaScript
function bytesToBase64(bytes) {
  const binString = Array.from(bytes, (byte) =>
    String.fromCodePoint(byte)
  ).join("");
  return btoa(binString);
}

const text = "a ✓";
const encoded = bytesToBase64(new TextEncoder().encode(text));
console.log(encoded); // Base64 of UTF-8 bytes
Try It Yourself

How It Works

  1. Encode UTF-8. TextEncoder turns text into bytes.
  2. Build a binary string. Each byte becomes one Latin-1 character.
  3. Call btoa. Now every character is a valid single byte.
  4. Modern option. Where supported, prefer Uint8Array.prototype.toBase64().

Browser Support

Window.btoa() is marked Baseline Widely available on MDN (since July 2015). Browser Window API — also available as a global in modern Node.js.

✓ Baseline · Widely available

btoa()

Encode binary strings to Base64 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 Base64 encoding of byte-safe / ASCII data. For Unicode text, convert to UTF-8 bytes first.

Frequently Asked Questions

It creates a Base64-encoded ASCII string from a binary string — each character is treated as one byte (code point 0–255). For plain ASCII text, you get a portable Base64 string.
No. MDN marks Window.btoa() 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. They are inverse operations for Latin-1 / byte-safe data.
The input contains a character whose code point is greater than 255 (for example many emoji). btoa() only accepts single-byte characters.
No. Base64 is encoding, not encryption. Anyone can decode with atob(). Never use btoa() alone to hide secrets.
Convert to UTF-8 bytes first (for example with TextEncoder), turn those bytes into a binary string, then call btoa(). Or use Uint8Array.prototype.toBase64() where available.

Next: clearInterval()

Stop a repeating timer started with setInterval().

clearInterval() →

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.

9 people found this page helpful