Encode or decode Base64 strings
Understand the mathematics of the 6-bit translation, the purpose of padding, and why Base64 is strictly for encoding—not encryption.
Computers natively handle data in 8-bit bytes. However, legacy systems (like early email routing) and some data formats (like JSON) are designed strictly for printable text. Base64 bridges this gap by translating binary bits into a safe, 64-character ASCII alphabet (A-Z, a-z, 0-9, +, /).
Because there are 64 characters in the alphabet, each character represents exactly 6 bits of data (26 = 64). The algorithm takes chunks of three 8-bit bytes (24 bits total) and splits them into four 6-bit pieces. Each piece is mapped to a Base64 character. As a result, Base64 encoding always increases file size by exactly 33%.
Base64 is NOT Encryption.
btoa (binary to ASCII) encodes, and atob (ASCII to binary) decodes.btoa() throws an error on Unicode strings (like emoji) because it only accepts 8-bit code points. This tool converts Unicode to UTF-8 byte arrays first before encoding.Standard Base64 uses the characters + and /. Unfortunately, these characters have special meaning in web URLs (spaces and directories).
URL-Safe Base64 (RFC 4648) replaces + with - (dash) and / with _ (underscore), and omits the = padding. This allows Base64 strings to be safely passed in URL query parameters.
Base64 processes data in 3-byte blocks. What happens if your input is only 1 or 2 bytes long?
= characters at the end of the string to tell the decoder exactly how many padding bytes were added, so the decoder knows to discard them.Why does encoding an image in Base64 make the web page load slower?