base64codingjavascriptapi

Base64: what is it and why is it needed?

Let's explain Base64 in simple words. Where it is used, how to encode and decode, examples in JavaScript.

Published February 23, 2026·Time to read: 8 min

What is Base64?

Base64 is a way of encoding binary data (bytes) into text format using 64 characters: A-Z, a-z, 0-9, +, /.

Why do you need Base64?

- Email attachments - MIME protocol uses Base64 for files

- Data URI — embedding images directly into HTML/CSS

- JWT tokens - header and payload are encoded in Base64

- API requests — transfer of binary data to JSON

- HTTP Basic AuthAuthorization: Basic dXNlcjpwYXNz

Example in JavaScript

// Encoding 
const encoded = btoa(''Hello, World!''); // "SGVsbG8sIFdvcmxkIQ==" 

// Decoding 
const decoded = atob(''SGVsbG8sIFdvcmxkIQ==''); // "Hello, World!" 

// For UTF-8 (Cyrillic, etc.) 
const encodeUTF8 = str ='>' btoa(unescape(encodeURIComponent(str))); 
const decodeUTF8 = str ='>' decodeURIComponent(escape(atob(str))); 

Data size

Base64 increases the data size by approximately 33%: every 3 bytes becomes 4 characters.

Try our free Base64 encoder - supports UTF-8 and Cyrillic.

We use cookies for analytics. Privacy Policy You can accept or decline non-essential tracking.