Which Characters Are Supported by the JavaScript btoa() Method?

The JavaScript btoa() method only supports characters that can be represented using a single byte (8-bits). This means that it only supports a total of 256 characters from the following character sets:

  1. 128 characters of the ASCII character set, and;
  2. 128 characters of the Latin-1 (ISO-8859-1) character set.

For example:

// ES5+
const encodedStr = btoa('foobar');
console.log(encodedStr); // 'Zm9vYmFy'

If you pass a string that is not in this range to the btoa() method, it will throw an error:

// ES5+
// Uncaught DOMException: Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.

To support multibyte strings, you can convert the original string to binary format so that it can be used with the btoa() method for Base64 encoding.

Hope you found this post useful. It was published . Please show your love and support by sharing this post.