Some browsers such as Firefox, Chrome, Safari, Opera and IE10+ can handle Base64 natively. Take a look at this Stackoverflow question. It's using btoa() and atob() functions.

For server-side JavaScript (Node), you can use Buffers to decode.

If you are going for a cross-browser solution, there are existing libraries like CryptoJS or code like:

http://ntt.cc/2008/01/19/base64-encoder-decoder-with-javascript.html (Archive)

With the latter, you need to thoroughly test the function for cross browser compatibility. And error has already been reported.

Answer from Jude Cooray on Stack Overflow
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-encode-and-decode-strings-with-base64-in-javascript
How To Encode and Decode Strings with Base64 in JavaScript | DigitalOcean
Learn how to encode and decode strings with Base64 in JavaScript. This guide covers btoa(), atob(), Buffer, modern APIs, and practical examples.
Top answer
1 of 16
291

Some browsers such as Firefox, Chrome, Safari, Opera and IE10+ can handle Base64 natively. Take a look at this Stackoverflow question. It's using btoa() and atob() functions.

For server-side JavaScript (Node), you can use Buffers to decode.

If you are going for a cross-browser solution, there are existing libraries like CryptoJS or code like:

http://ntt.cc/2008/01/19/base64-encoder-decoder-with-javascript.html (Archive)

With the latter, you need to thoroughly test the function for cross browser compatibility. And error has already been reported.

2 of 16
144

Internet Explorer 10+

// Define the string
var string = 'Hello World!';

// Encode the String
var encodedString = btoa(string);
console.log(encodedString); // Outputs: "SGVsbG8gV29ybGQh"

// Decode the String
var decodedString = atob(encodedString);
console.log(decodedString); // Outputs: "Hello World!"

Cross-Browser

Re-written and modularized UTF-8 and Base64 Javascript Encoding and Decoding Libraries / Modules for AMD, CommonJS, Nodejs and Browsers. Cross-browser compatible.


with Node.js

Here is how you encode normal text to base64 in Node.js:

//Buffer() requires a number, array or string as the first parameter, and an optional encoding type as the second parameter. 
// Default is utf8, possible encoding types are ascii, utf8, ucs2, base64, binary, and hex
var b = Buffer.from('JavaScript');
// If we don't use toString(), JavaScript assumes we want to convert the object to utf8.
// We can make it convert to other formats by passing the encoding type to toString().
var s = b.toString('base64');

And here is how you decode base64 encoded strings:

var b = Buffer.from('SmF2YVNjcmlwdA==', 'base64')
var s = b.toString();

with Dojo.js

To encode an array of bytes using dojox.encoding.base64:

var str = dojox.encoding.base64.encode(myByteArray);

To decode a base64-encoded string:

var bytes = dojox.encoding.base64.decode(str)

bower install angular-base64

<script src="bower_components/angular-base64/angular-base64.js"></script>

angular
    .module('myApp', ['base64'])
    .controller('myController', [

    '$base64', '$scope', 
    function($base64, $scope) {
    
        $scope.encoded = $base64.encode('a string');
        $scope.decoded = $base64.decode('YSBzdHJpbmc=');
}]);

But How?

If you would like to learn more about how base64 is encoded in general, and in JavaScript in-particular, I would recommend this article: Computer science in JavaScript: Base64 encoding

🌐
Base64 Decode
base64decode.org
Base64 Decode and Encode - Online
Decode each line separately: The ... their Base64-encoded forms. Prior to decoding, all non-encoded whitespaces are stripped from the input to safeguard the input's integrity. This option is useful if you intend to decode multiple independent data entries that are separated by line breaks. Live mode: When you turn on this option the entered data is decoded immediately with your browser's built-in JavaScript functions, ...
🌐
Decodebase64
decodebase64.com
Decode and Encode Base64 (using JavaScript)
This is a simple online base 64 encoder and decoder. This page was designed to be helpful to developers and anyone doing programming work. Base64 is a common format used for the web and email. It allows binary data to be transmitted in plain text format without risk of the data being clobbered ...
🌐
Medium
medium.com › @sunil17bbmp › how-to-encode-and-decode-base64-strings-in-javascript-c94647409e0d
How to Encode and Decode Base64 Strings in JavaScript | by Code With Sunil | Code Smarter, not harder | Medium
March 18, 2026 - In this article, we’ll explain how to use btoa() to encode strings into Base64 and atob() to decode them back to their original form.
🌐
Medium
medium.com › @olenkadark › how-to-encode-and-decode-strings-with-base64-in-javascript-100ec741873d
How To Encode and Decode Strings with Base64 in JavaScript
May 31, 2024 - Learning to handle character encoding properly was a game-changer. What goes up must come down, right? The same is true for decoding. JavaScript provides a atob()function to decode your Base64 string back to its original form:
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-encode-decode-a-string-to-base64
JavaScript | Encode/Decode a string to Base64 - GeeksforGeeks
May 28, 2024 - The Cross-Browser Method is used as a JavaScript library to encode/decode a string in any browser. Example 3: This examples encodes the string "This is GeeksForGeeks" by creating a Base64 object.
Top answer
1 of 16
1415

You can use btoa() and atob() to convert to and from base64 encoding.

There appears to be some confusion in the comments regarding what these functions accept/return, so…

  • btoa() accepts a “string” where each character represents an 8-bit byte – if you pass a string containing characters that can’t be represented in 8 bits, it will probably break. This isn’t a problem if you’re actually treating the string as a byte array, but if you’re trying to do something else then you’ll have to encode it first.

  • atob() returns a “string” where each character represents an 8-bit byte – that is, its value will be between 0 and 0xff. This does not mean it’s ASCII – presumably if you’re using this function at all, you expect to be working with binary data and not text.

See also:

  • How do I load binary image data using Javascript and XMLHttpRequest?

Most comments here are outdated. You can probably use both btoa() and atob(), unless you support really outdated browsers.

Check here:

  • https://caniuse.com/?search=atob
  • https://caniuse.com/?search=btoa

In 2025, all "evergreen" browsers offer toBase64 and fromBase64 to convert a Uint8Array to and from Base64 string. If your input is a Unicode string, not raw bytes, you can convert it to and from Uint8Array (of UTF-8 bytes) by TextEncoder and TextDecoder. See also another answer about this.

2 of 16
330

From here:

/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/
var Base64 = {

    // private property
    _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    // public method for encoding
    encode : function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = Base64._utf8_encode(input);

        while (i < input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output +
            this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
            this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
        }
        return output;
    },

    // public method for decoding
    decode : function (input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;

        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

        while (i < input.length) {

            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;

            output = output + String.fromCharCode(chr1);

            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }
        }

        output = Base64._utf8_decode(output);

        return output;
    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }
        }
        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }
        }
        return string;
    }
}

Also, search for "JavaScript base64 encoding" turns up a lot of other options, and the above was the first one.

Find elsewhere
🌐
freeCodeCamp
freecodecamp.org › news › encode-decode-html-base64-using-javascript
How to Encode and Decode HTML Base64 using JavaScript – JS Encoding Example
November 7, 2024 - You can encode a string to base64 in JavaScript using the btoa() function and decode a base64 string using atob() function.
🌐
DEV Community
dev.to › _d7eb1c1703182e3ce1782 › how-to-encode-and-decode-base64-in-javascript-4bcj
How to Encode and Decode Base64 in JavaScript - DEV Community
March 25, 2026 - // Encode a string to Base64 const encoded = btoa('Hello, World!'); console.log(encoded); // "SGVsbG8sIFdvcmxkIQ==" // Decode Base64 back to string const decoded = atob('SGVsbG8sIFdvcmxkIQ=='); console.log(decoded); // "Hello, World!"
🌐
Akamai
akamai.com › cloud › guides › javascript-base-64-decode
How to Use JavaScript Base 64 to Decode and Encode | Linode Docs
March 29, 2023 - ... JavaScript can also decode Base64 text. The procedure to decode a Base64 representation back into the original is very similar. Use the atob function and pass it the Base64 string.
🌐
Stack Abuse
stackabuse.com › encoding-and-decoding-base64-strings-in-node-js
Encoding and Decoding Base64 Strings in Node.js
August 22, 2017 - It is a very useful format for communicating between one or more systems that cannot easily handle binary data, like images in HTML markup or web requests. In Node.js, the Buffer object can be used to encode and decode Base64 strings to and from many other formats, allowing you to easily convert ...
🌐
PhoenixNAP
phoenixnap.com › home › kb › devops and development › base64 decode and encode in javascript explained
Base64 Decode and Encode in JavaScript Explained
November 18, 2025 - Unicode handling issues. JavaScript strings are UTF-16 encoded. However, Base64 operates on bytes, not characters. This means you must convert text to bytes (UTF-8) before encoding, and back again after decoding.
🌐
Hostman
hostman.com › tutorials › how-to-encode-and-decode-strings-using-base64-in-javascript
Javascript Base64: How To Encode and Decode Strings
The encoding algorithm splits data into 3-byte blocks and transforms them into four characters from a special alphabet. This ensures compatibility with systems that support only text content. Built-in JavaScript functions like btoa() and atob() simplify encoding and decoding, though you may ...
🌐
Base64
base64.dev › home › articles › base64 in javascript
Base64 Encoding & Decoding in JavaScript — base64.dev
July 3, 2026 - // Encode Unicode string to Base64 ... string function decodeBase64(base64) { const binary = atob(base64); const bytes = Uint8Array.from(binary, c => c.charCodeAt(0)); return new TextDecoder().decode(bytes); } console.log...
🌐
Alanreed
base64.alanreed.org
Javascript Base64 Encode Online | AlanReed.org
To Encode, put your plaintext in the first textarea and click "Encode." To Decode, put your base64 string in the second textarea and click "Decode."
🌐
CoreUI
coreui.io › answers › how-to-decode-a-base64-string-in-javascript
How to decode a base64 string in JavaScript · CoreUI
May 21, 2026 - Use atob() function to decode base64-encoded strings back to their original text format in JavaScript efficiently.
🌐
Base64 Encode
base64encode.org
Base64 Encode and Decode - Online
Enable this option to encode into ... 4648 / Base64URL) where the "+" and "/" characters are respectively replaced by "-" and "_", as well as the padding "=" signs are omitted. Live mode: When you turn on this option the entered data is encoded immediately with your browser's built-in JavaScript functions, ...