4.2 KiB
| title | chunk | source | category | tags | date_saved | instance |
|---|---|---|---|---|---|---|
| Base64 - Glossary | MDN | 2/4 | https://developer.mozilla.org/en-US/docs/Glossary/Base64 | reference | web, html, css, javascript, documentation | 2026-05-05T05:24:47.878610+00:00 | kb-cron |
In the first two cases, the characters may have 4 or 2 extra trailing bits that don't represent any data. In this case, RFC 4648 requires encoders to set these bits to zero and decoders to optionally throw an error if they are not zero. For example, if the encoded data is a single byte 0b01010101, then it needs two characters 0b010101 (V) and 0b010000 (Q), where the second character has 4 trailing bits set to zero. Decoding VR== (where the second character represents 0b010001) technically results in the same byte 0b01010101, but the decoder may throw an error due to the trailing bits not being zero.
JavaScript support
The Uint8Array class provides the Uint8Array.fromBase64(), Uint8Array.prototype.toBase64(), and Uint8Array.prototype.setFromBase64() methods for conversion to/from base64 strings.
Browsers also natively provide two JavaScript functions for decoding and encoding Base64 strings:
Window.btoa()(also available in workers): creates a Base64-encoded ASCII string from a string of binary data ("btoa" should be read as "binary to ASCII").Window.atob()(also available in workers): decodes a Base64-encoded string ("atob" should be read as "ASCII to binary").
Note: Base64 is a binary encoding rather than a text encoding, but btoa and atob were added to the web platform before it supported binary data types. As a result, the two functions use strings to represent binary data, with the code point of each character representing the value of each byte. This has led to a common misconception that btoa can be used to encode arbitrary text data — for example, creating a Base64 data: URL of a text or HTML document.
However, the byte-to-code-point correspondence only reliably holds true for code points up to 0x7f. Furthermore, code points over 0xff will cause btoa to throw an error due to exceeding the maximum value for 1 byte. The Window.btoa() "Unicode strings" section details how to work around this limitation when encoding arbitrary Unicode text.