What is Base64 and Why It Exists
You'll learn what Base64 encoding is, why we convert binary data to text, and where it's commonly used in JavaScript applications.
The Core Problem
Binary data (like images, audio files, or raw ArrayBuffer data) is made up of bytes that can represent any value from 0 to 255. Many systems—like JSON, XML, email protocols, and URLs—were designed to handle text only, not arbitrary binary bytes. If you try to send binary data through these text-based channels, some bytes might be interpreted as control characters or special symbols, corrupting your data.
What Base64 Does
Base64 encoding converts binary data into a text string using only 64 safe, printable ASCII characters: A-Z, a-z, 0-9, +, and / (plus = for padding). Think of it like translating a book written in an alien alphabet into English—everyone can read English, even if it takes more space.
The trade-off? Base64-encoded data is about 33% larger than the original binary, but it's guaranteed to survive transmission through text-only systems.
Common Use Cases
- Embedding images in HTML/CSS: Data URLs like
<img src="data:image/png;base64,iVBORw0KG..."> - Sending binary files in JSON: APIs often encode files as Base64 strings
- Storing binary data in text databases: When you need to save bytes in a text field
- Email attachments: MIME uses Base64 to encode files
Base64 isn't encryption or compression—it's purely a representation change for compatibility.
Key Takeaway: Base64 encoding converts binary data into a text-safe format using 64 printable characters, enabling binary data to pass through text-only systems like JSON, HTML, and email at the cost of ~33% size increase.