Skip to content
the poor coder

js base64 encode

Poor Man
Published
Reading time
1 min read
On this page

What is JS Base64 encode?#

Base64 is a way to encode binary data into ASCII text format. It is commonly used for transmitting data over the internet, such as sending images or files via email. JavaScript provides a built-in function called btoa() to encode data to Base64 format.

How to use btoa() function?#

The btoa() function takes a string as input and returns a Base64 encoded string. Here's an example:

Text

      let originalString = "Hello, World!";
      let encodedString = btoa(originalString);
      
      console.log(encodedString);
      // Output: "SGVsbG8sIFdvcmxkIQ=="
    

The encoded string can now be safely transmitted over the internet without the risk of data loss or corruption. To decode the string back into its original form, use the atob() function.

Alternative method: Using a third-party library#

There are also many third-party libraries available that can encode and decode strings to Base64 format. One such library is js-base64, which provides a more feature-rich Base64 encoder and decoder than the built-in JavaScript functions.

Here's an example using js-base64:

Text

      let originalString = "Hello, World!";
      let encodedString = Base64.encode(originalString);
      
      console.log(encodedString);
      // Output: "SGVsbG8sIFdvcmxkIQ=="
    

In addition to encoding and decoding strings, js-base64 also supports encoding and decoding binary data, URL-safe encoding, and decoding, and more.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC