Base64 encoding and decoding
The atob() method of the Window interface decodes a string of data which has been encoded using Base64 encoding. You can use the Window.btoa() method to encode and transmit data which may otherwise cause communication problems, then transmit it and use the atob() method to decode the data again. For example, you can encode, transmit, and decode control characters such as ASCII values 0 through 31.
Also consider using the Uint8Array.fromBase64() method, which creates a Uint8Array object from a base64-encoded string. It results in a byte array, which is easier to work with than a string containing raw bytes.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 4 | 12 | 1 | 3 | 18 | 1 | |
worker_support Available in workers | 30 | 12 | 4 | 10 | 30 | 10 |
| The btoa() method of the Window interface creates a Base64-encoded ASCII string from a binary string (i.e., a string in which each character in the string is treated as a byte of binary data). | 4 | 12 | 1 | 3 | 18 | 1 |
worker_support Available in workers | 30 | 12 | 4 | 10 | 30 | 10 |
Syntax
const encodedData = window.btoa("Hello, world"); // encode a string
const decodedData = window.atob(encodedData); // decode the string Use cases
-
Using Base64 encoding and decoding
The atob() method of the Window interface decodes a
Cautions
- May not be supported in older browsers.
Implementation notes
- Some APIs require a secure context (HTTPS) or user activation. Check the requirements before use.