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
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

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Glossary › Base64
Base64 - Glossary | MDN
Decoding VR== (where the second character represents 0b010001) technically results in the same byte 0b01010101, but the decoder may throw an error due to the trailing bits not being zero. The Uint8Array class provides the Uint8Array.fromBase64(), Uint8Array.prototype.toBase64(), and Uint8Array.prototype.setFromBase64() methods for conversion to/from base64 strings. Browsers also natively provide two JavaScript functions for decoding and encoding Base64 strings: Window.btoa() (also available in workers): creates a Base64-encoded ASCII string from a string of binary data ("btoa" should be read as "binary to ASCII").
🌐
Akamai
akamai.com › cloud › guides › javascript-base-64-decode
JavaScript Decode Base64 String - A Developer's Guide
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. atob should produce the original string.
🌐
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, ...
🌐
Jam
jam.dev › utilities › base-64-encoder
Base64 Encoder/Decoder | Free, Open Source & Ad-free
ASCII Decoding: Converts ASCII strings back to their original binary form, ensuring accurate data reconstruction. In JavaScript, Base64 encoding and decoding can be done using the built-in btoa and atob functions.
🌐
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.
Find elsewhere
🌐
npm
npmjs.com › package › js-base64
js-base64 - npm
August 17, 2026 - Use Base64.atob(pngBase64) instead. Base64.decode() decodes to UTF-8 string while Base64.atob() decodes to bytes, which is compatible to browser built-in atob() (Which is absent in node.js).
      » npm install js-base64
    
Published: Sep 19, 2026
Version: 3.9.4
🌐
GitHub
github.com › dankogai › js-base64
GitHub - dankogai/js-base64: Base64 implementation for JavaScript · GitHub
Use Base64.atob(pngBase64) instead. Base64.decode() decodes to UTF-8 string while Base64.atob() decodes to bytes, which is compatible to browser built-in atob() (Which is absent in node.js).
Author: dankogai
🌐
GeeksforGeeks
geeksforgeeks.org › node.js › how-base64-encoding-and-decoding-is-done-in-node-js
How Base64 Encoding and Decoding is Done in Node.js ? - GeeksforGeeks
January 8, 2025 - Base64 encoding and decoding in Node.js are straightforward with the Buffer class. This capability is particularly useful for handling binary data that needs to be transmitted over text-based protocols or stored in text formats.
🌐
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.
🌐
W3Schools
w3schools.com › tools › tool_base64.php
Base64 Encode/Decode - W3Schools
Encode text to Base64 or decode Base64 to text. Supports file/image encoding too.
🌐
Medium
abhinovator.medium.com › encode-and-decode-in-javascript-base64-87e975a1f173
Encode and Decode in Javascript (Base64) | by Abhishek Mallick | Medium
July 8, 2020 - Now let us try to do some Base64 Encoding (btoa) and some Base64 Decoding (atob) using Javascript:
🌐
Online Tools
emn178.github.io › online-tools › base64_decode.html
Base64 Decode - Online Tools
The bytes fbff encode as +/8= in standard Base64 and -_8= in Base64URL; both are accepted by this page’s Standard decoder.
🌐
GitHub
gist.github.com › pedrovasconcellos › 2ac103ca075b08e8efee75d69b7c4ba3
Javascript Base64 UTF8 encoding and decoding · GitHub
Javascript Base64 UTF8 encoding and decoding. GitHub Gist: instantly share code, notes, and snippets.
🌐
Base64.Guru
base64.guru › home › developers › javascript › examples
Convert Base64 to PDF in JavaScript | Examples | JavaScript | Developers | Base64
Do you have a Base64 string and do not know how to convert it to PDF using JavaScript? Do not worry! I am here to help you. I will show you some practical examples how to decode Base64 to PDF using the atob function and get some information about it.
🌐
n8n
community.n8n.io › questions
How to decode base64 - Questions - n8n Community
July 26, 2022 - Hi all Describe the issue/error/question Following a HTTP request, I get a JSON with loads of fields. The content of one of the fields that I need is encoded in base64. I have NO idea how to decode it! I am a noob in coding, so I have no idea how to use javascript or other to decode the base64.
🌐
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.
🌐
GitHub
gist.github.com › ludo6577 › 94ab44dc7bca992da926
Base64 encode decode .js · GitHub
Base64 encode decode .js · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters ·