kb/data/developer.mozilla.org/en-US/docs/Glossary/Base64-1.md

27 lines
4.2 KiB
Markdown

---
title: "Base64 - Glossary | MDN"
chunk: 2/4
source: "https://developer.mozilla.org/en-US/docs/Glossary/Base64"
category: "reference"
tags: "web, html, css, javascript, documentation"
date_saved: "2026-05-05T05:24:47.878610+00:00"
instance: "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](https://datatracker.ietf.org/doc/html/rfc4648#section-3.5) 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](https://developer.mozilla.org/en-US/docs/Glossary/Base64#javascript_support)
The [`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) class provides the [`Uint8Array.fromBase64()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array/fromBase64), [`Uint8Array.prototype.toBase64()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array/toBase64), and [`Uint8Array.prototype.setFromBase64()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array/setFromBase64) methods for conversion to/from base64 strings.
Browsers also natively provide two JavaScript functions for decoding and encoding Base64 strings:
* [`Window.btoa()`](https://developer.mozilla.org/en-US/docs/Web/API/Window/btoa) (also [available in workers](https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/btoa "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()`](https://developer.mozilla.org/en-US/docs/Web/API/Window/atob) (also [available in workers](https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/atob "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](https://developer.mozilla.org/en-US/docs/Glossary/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()`](https://developer.mozilla.org/en-US/docs/Web/API/Window/btoa) "[Unicode strings](https://developer.mozilla.org/en-US/docs/Web/API/Window/btoa#unicode_strings)" section details how to work around this limitation when encoding arbitrary Unicode text.
## [See Also](https://developer.mozilla.org/en-US/docs/Glossary/Base64#see_also)
* JavaScript APIs:
* [`Window.atob()`](https://developer.mozilla.org/en-US/docs/Web/API/Window/atob) (also [available in workers](https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/atob "available in workers"))
* [`Window.btoa()`](https://developer.mozilla.org/en-US/docs/Web/API/Window/btoa) (also [available in workers](https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/btoa "available in workers"))
* [`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array)
* [Data URLs](https://developer.mozilla.org/en-US/docs/Web/URI/Reference/Schemes/data)
* [Base64](https://en.wikipedia.org/wiki/Base64) on Wikipedia
* Base64 Algorithm described in [RFC 4648](https://datatracker.ietf.org/doc/html/rfc4648)