Languages
[Edit]
EN

JavaScript - Base64 encode and decode

0 points
Created by:
Inayah-Alexander
767

In this article, we would like to show you how to encode and decode Base64 in JavaScript.

Quick solution:

// ONLINE-RUNNER:browser;

const text = 'some text...';

const encoded = btoa(text);
console.log('Encoded text: ' + encoded);  // c29tZSB0ZXh0Li4u

const decoded = atob(encoded);
console.log('Decoded text: ' + decoded);  // some text...

 

1. Encode

In this example, we use btoa() method to create a Base64-encoded ASCII string.

// ONLINE-RUNNER:browser;

const text = 'some text...';

const encoded = btoa(text);
console.log(encoded);  // c29tZSB0ZXh0Li4u

Output:

c29tZSB0ZXh0Li4u

2. Decode

In this example, we use atob() method to decode a string of data which has been encoded using Base64 encoding.

// ONLINE-RUNNER:browser;

const encoded = 'c29tZSB0ZXh0Li4u';

const decoded = atob(encoded);
console.log(decoded);  // some text...

Output:

some text...

References

  1. atob() - Web APIs | MDN
  2. btoa() - Web APIs | MDN

Alternative titles

  1. JavaScript - Base 64 encode and decode
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join