WorkerGlobalScope: btoa() method - Web APIs | MDN

Syntax

Parameters

stringToEncode

The binary string to encode.

Return value

An ASCII string containing the Base64 representation of stringToEncode.

Exceptions

InvalidCharacterError DOMException

The string contained a character that did not fit in a single byte. See "Unicode strings" below for more detail.

Examples

js

const encodedData = self.btoa("Hello, world"); // encode a string
const decodedData = self.atob(encodedData); // decode the string

Unicode strings

Base64, by design, expects binary data as its input. In terms of JavaScript strings, this means strings in which the code point of each character occupies only one byte. So if you pass a string into btoa() containing characters that occupy more than one byte, you will get an error, because this is not considered binary data.

For more information and workarounds see Window.btoa().

Specifications

Specification
HTML
# dom-btoa-dev

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on by MDN contributors.