Understanding the Base64 Format in 2 Minutes

NicolasBrondinBernard

Author
@NicolasBrondinBernard

Base64 is an encoding format, but what is its purpose, and in what context is it useful to use it?

Article published on 24/11/2024, last updated on 10/08/2026

You've surely already heard about Base64 around topics like:

  • JWT tokens
  • File transfer or storage
  • Data hashing
  • Certificates
  • Etc…

In this article we'll understand the nature of this format, its advantages and how to use it with some code examples.

What is Base64?

The simplest definition we could give is that Base64 is a data encoding format, just like binary, hexadecimal, etc…

The difference with other encodings comes from the set of alphanumeric characters used to represent this famous data.

In binary we use characters from 0 to 1, in hexadecimal we use characters 0 to 9 and A to F, and in Base64, we use:

  • Characters from a to z (lowercase)
  • Characters from A to Z (uppercase)
  • Digits from 0 to 9
  • Special characters “+” and “/”

Note that we also use the special character “=” which is used to pad the missing bits at the end of the sequence.

It's from the number of characters used for encoding that this format gets its name “Base64”, for 64 characters used

This format is used to encode raw (or “binary”) data into a format in the shape of a character string. For example, the following text:

Lorem ipsum dolores sit

Becomes in Base64:

TG9yZW0gaXBzdW0gZG9sb3JlcyBzaXQ=

What's the point?

The question that often comes up is: why use Base64 rather than binary, or hexadecimal representation?

The main appeal of Base64 lies in its “compatibility”

This format allows binary data (files for example) to be transmitted through protocols or file formats that are designed to carry plain text.

Originally, Base64 was invented to transmit files in emails, without risking the file's binary data being misinterpreted by the SMTP protocol.

Since Base64 consists only of common, visible, and even printable characters, the data is compatible with any system that transports (or stores) text.

What's the downside?

Due to its format, a file's data converted to Base64 is generally 33-37% larger than the binary version.

This is why this encoding should be used only when there is a real benefit, such as in HTTP headers, for JWT tokens for example!

nicolasbrondinbernard_A_vintage_notepad._100_white_background.__88c54678-1ff4-497b-be1b-cf1f4e05948d.png

How to use it?

In JavaScript

There are dedicated functions for Base64 encoding in JavaScript, available in the browser but also in NodeJS and all other environments.

To encode data in Base64, we'll use btoa(), like this:

const data = "hello world";
const base64 = btoa(data);
// "aGVsbG8gd29ybGQ="

Note that the function name stands for “Binary” to “Ascii”

And to decode a Base64 string, we'll use the reverse function atob():

const base64 = "aGVsbG8gd29ybGQ=";
const data = btoa(base64);
// "hello world"

In other languages

Base64 is widely used, so you'll find utilities in all major languages to help you:


Finished reading this article?
Our newsletter

No spam. Only free content, news, and ever more resources to level up your skills!

Join +1500 developers

Comments (0)

to leave a comment

No comments yet