W3cubDocs

/DOM

WindowOrWorkerGlobalScope.btoa

The WindowOrWorkerGlobalScope.btoa() method creates a base-64 encoded ASCII string from a String object in which each character in the string is treated as a byte of binary data.

Note: Since this function treats each character as a byte of binary data, regardless of the number of bytes which actually make up the character, an InvalidCharacterError exception is thrown if any character's code point is outside the range 0x00 to 0xFF. See Unicode strings for an example demonstrating how to encode strings with characters outside the 0x00 to 0xFF range

Syntax

var encodedData = scope.btoa(stringToEncode);

Parameters

stringToEncode
A string whose characters each represent a single byte of binary data to be encoded into ASCII.

Return value

A string containing the Base64 representation of stringToEncode.

Exceptions

Example

var encodedData = window.btoa('Hello, world'); // encode a string
var decodedData = window.atob(encodedData); // decode the string

Notes

You can use this method to encode data which may otherwise cause communication problems, transmit it, then use the atob() method to decode the data again. For example, you can encode control characters such as ASCII values 0 through 31.

btoa() is also available to XPCOM components implemented in JavaScript, even though Window is not the global object in components.

Unicode strings

In most browsers, calling btoa() on a Unicode string will cause an InvalidCharacterError exception.

One option is to escape any extended characters so that the string you actually encode is an ASCII representation of the original. Consider this example, noted by Johan Sundström:

// ucs-2 string to base64 encoded ascii
function utoa(str) {
    return window.btoa(unescape(encodeURIComponent(str)));
}
// base64 encoded ascii to ucs-2 string
function atou(str) {
    return decodeURIComponent(escape(window.atob(str)));
}
// Usage:
utoa('✓ à la mode'); // 4pyTIMOgIGxhIG1vZGU=
atou('4pyTIMOgIGxhIG1vZGU='); // "✓ à la mode"

utoa('I \u2661 Unicode!'); // SSDimaEgVW5pY29kZSE=
atou('SSDimaEgVW5pY29kZSE='); // "I ♡ Unicode!"

A better, more faithful and less expensive solution is to use typed arrays to do the conversion.

Specifications

Specification Status Comment
HTML Living Standard
The definition of 'WindowOrWorkerGlobalScope.btoa()' in that specification.
Living Standard Method moved to the WindowOrWorkerGlobalScope mixin in the latest spec.
HTML 5.1
The definition of 'WindowBase64.btoa()' in that specification.
Recommendation Snapshot of HTML Living Standard. No change.
HTML5
The definition of 'WindowBase64.btoa()' in that specification.
Recommendation Snapshot of HTML Living Standard. Creation of WindowBase64 (properties where on the target before it).

Browser compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
Basic support Yes Yes 1
1
52
btoa() now defined on WindowOrWorkerGlobalScope mixin.
10 Yes Yes
Mobile
Android webview Chrome for Android Edge Mobile Firefox for Android Opera for Android iOS Safari Samsung Internet
Basic support Yes Yes ? 4
4
52
atob() now defined on WindowOrWorkerGlobalScope mixin.
? Yes ?

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/btoa