Join MDN and developers like you at Mozilla's View Source conference, 12-14 September in Berlin, Germany. Learn more at https://viewsourceconf.org

This article needs a technical review. How you can help.

The WindowBase64.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.

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 = window.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 window.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 window.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
WHATWG HTML Living Standard
The definition of 'WindowBase64.btoa()' in that specification.
Living Standard No change since the latest snapshot, HTML5.1.
HTML5.1
The definition of 'WindowBase64.btoa()' in that specification.
Working Draft Snapshot of WHATWG HTML Living Standard. No change.
HTML5
The definition of 'WindowBase64.btoa()' in that specification.
Recommendation Snapshot of WHATWG HTML Living Standard. Creation of WindowBase64 (properties where on the target before it).

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support (Yes) 1.0 (1.7 or earlier)[1] 10 (Yes) (Yes)
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) 1.0 (1) No support ? (Yes)

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

See also