Skip to content

Converting to and from Base64 in JavaScript

JavaScript, Base64, Encoding, Decoding1 min read

Encoding binary data, such as images or files, into a human-readable format is a common requirement in web development. Base64 encoding provides a way to represent binary data using only ASCII characters, making it suitable for storage or transmission via text-based protocols. In JavaScript, you can easily convert data to and from Base64 using built-in functions and libraries. In this article, we'll explore the process of converting to and from Base64 in JavaScript with some practical examples.

Encoding to Base64

To encode data to Base64 in JavaScript, you can use the btoa() function, which stands for "binary to ASCII." This function takes a string parameter representing the data you want to encode and returns the Base64-encoded string.

Here's an example demonstrating how to encode a string:

1const originalString = "Hello, World!";
2const encodedString = btoa(originalString);
3console.log(encodedString); // SGVsbG8sIFdvcmxkIQ==

In this example, the original string "Hello, World!" is encoded using btoa(). The resulting Base64-encoded string, "SGVsbG8sIFdvcmxkIQ==," is printed to the console.

Decoding from Base64

Conversely, to decode a Base64-encoded string back into its original form, JavaScript provides the atob() function. This function takes a Base64-encoded string and returns the decoded binary data as a string.

Let's decode the previously encoded string:

1const decodedString = atob(encodedString);
2console.log(decodedString); // Hello, World!

By calling atob() with the encoded string "SGVsbG8sIFdvcmxkIQ==," we retrieve the original string "Hello, World!".

Working with Binary Data

In addition to encoding and decoding strings, you can also work with binary data directly. Suppose you have an image file and want to convert it to Base64 for display or transmission. In such cases, you can use the FileReader API in JavaScript to read the file contents as a Data URL and extract the Base64-encoded data.

Here's an example that demonstrates how to convert an image file to Base64:

1const fileInput = document.getElementById("file-input");
3fileInput.addEventListener("change", (event) => {
4 const file =[0];
5 const reader = new FileReader();
7 reader.onload = () => {
8 const base64Data = reader.result.split(",")[1];
9 console.log(base64Data); // Base64-encoded image data
10 };
12 reader.readAsDataURL(file);

In this example, we listen for changes on a file input element (fileInput) and read the selected file using a FileReader. Once the file is loaded, we extract the Base64-encoded image data from the reader.result by splitting the Data URL and retrieving the second part.

Bonus Round: Using Third Party Libraries

While JavaScript provides built-in functions for basic Base64 encoding and decoding, there are also libraries available that offer additional features and functionality. One popular library is js-base64, which provides a comprehensive set of methods for working with Base64.

To use js-base64, start by installing and importing the library. Then, you can utilize its encoding and decoding methods as follows:

1import { Base64 } from "js-base64";
3const originalString = "Hello, World!";
4const encodedString = Base64.encode(originalString);
5console.log(encodedString); // SGVsbG8sIFdvcmxkIQ==
7const decodedString = Base64.decode(encodedString);
8console.log(decodedString); // Hello, World!

By importing Base64 from the library, you can easily encode and decode data using the provided methods.