A virtual teacher who reveals to you the great secrets of Base64

JavaScript Base64

JavaScript (abbreviated as JS) is a multi-paradigm programming language which supports object-oriented, imperative and functional styles. As well as HTML and CSS, JavaScript is most often used in web development (of course, it is not only limited to this). In addition to a huge number of features, it provides built-in functions to encode and decode data using the Base64 algorithm.

To encode data to Base64 in JavaScript, use the function btoa:

var b64 = btoa('guru');
console.log(b64); //-> "Z3VydQ=="

And of course, the reverse process — to decode Base64 values use the function atob:

var str = atob('Z3VydQ==');
console.log(str); //-> "guru"

Although there is nothing complicated about these functions, you should remember the following:

  1. Some old browsers don’t support JavaScript Base64 functions. This is why, be sure that you want to support only Chrome 4+, Edge 12+, Firefox 2+, Internet Explorer 10+, and Safari 3.1+, otherwise you must use a polyfill.

  2. As a rule, the built-in JavaScript Base64 functions will not be able to encode and decode Unicode strings. Therefore, if you need to support them you should apply some “hacks”.

  3. There are some objects that support methods for converting values to a Data URIs. Thanks to them, you can get Base64 values by simply removing the data URI prefix, which may be faster than using btoa.

To get a closer look at JavaScript Base64 functions, please consider the following examples:

Comments (1)

I hope you enjoy this discussion. In any case, I ask you to join it.

  • funny,
    I can help you a problem.
    I want to load page on mobile

    object.iframe-view(style="height: 600px;" data=`data:application/pdf;base64,${dataPostRes.data.base64}`type="application/pdf")


    how to display it in mobile when responsive no display.
Add new comment

If you have any questions, remarks, need help, or just like this page, please feel free to let me know by leaving a comment using the form bellow.
I will be happy to read every comment and, if necessary, I will do my best to respond as quickly as possible. Of course, spammers are welcome only as readers.