Base64 is a binary-to-text encoding scheme defined by RFC 4648 designed to transport arbitrary binary data (such as compiled bytes, cryptographic keys, and raster images) across text-only protocols like HTTP headers, JSON payloads, and MIME emails without data corruption.
Digital text and binary files are stored in 8-bit octets (bytes), allowing $2^8 = 256$ possible values per byte. Many of these values represent non-printable control characters (such as null bytes, line feeds, and bell alerts) which legacy network protocols could alter or drop.
Base64 solves this by taking groups of 3 bytes (24 bits) and dividing them into 4 sextets (6 bits each). Since $2^6 = 64$, each 6-bit chunk maps perfectly to an index within an ASCII-safe 64-character alphabet:
A–Z (indexes 0–25)a–z (indexes 26–51)0–9 (indexes 52–61)+ (index 62) and / (index 63)Because 3 original bytes become 4 encoded ASCII characters, Base64 encoding inherently increases data footprint by exactly $4/3 \approx 133.33\%$. When encoding an image into an inline CSS background or HTML Data URI, expect the file payload to grow by approximately one-third.
Standard Base64 contains the characters + and /. In URLs, + is frequently interpreted as a space, and / acts as a directory path separator. URL-Safe Base64 (RFC 4648 §5) substitutes + with - (hyphen) and / with _ (underscore), while optionally omitting trailing = padding characters. This format is heavily utilized in JWT tokens and URL query parameters.
TextEncoder to properly encode Unicode codepoints (like emojis and non-Latin scripts) into UTF-8 byte streams before Base64 conversion, preventing the classic JavaScript btoa character-out-of-range exception.