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
🌐
npm
npmjs.com › package › js-base64
js-base64 - npm
2 weeks ago - Yet another Base64 transcoder in pure-JS. Latest version: 3.9.4, last published: 12 days ago. Start using js-base64 in your project by running `npm i js-base64`. There are 4095 other projects in the npm registry using js-base64.
      » npm install js-base64
    
Published: Sep 19, 2026
Version: 3.9.4
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Glossary › Base64
Base64 - Glossary | MDN
Note: Base64 is a binary encoding rather than a text encoding, but btoa and atob were added to the web platform before it supported binary data types. As a result, the two functions use strings to represent binary data, with the code point of each character representing the value of each byte.
🌐
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

🌐
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.
Find elsewhere
🌐
Akamai
akamai.com › cloud › guides › javascript-base-64-decode
JavaScript Decode Base64 String - A Developer's Guide
March 29, 2023 - Base64 allows channels that only support text to store and transmit non-text files. Because the Base64 character set only contains common text characters, it is always safe to use.
🌐
Socket
socket.dev › npm › package › js-base64
js-base64 - npm Package Security Analysis - Socket
Yet another Base64 transcoder in pure-JS. Version: 3.9.4 was published by dankogai. Start using Socket to analyze js-base64 and its dependencies to se...
🌐
GitHub
github.com › juanelas › base64
juanelas/base64
Base64 for both node.js and browser javascript. It supports URL-safe encoding and enabling/disabling padding.
Author: juanelas
🌐
jsDelivr
jsdelivr.com › package › npm › base-64
base-64 CDN by jsDelivr - A CDN for npm and GitHub
October 17, 2020 - A free, fast, and reliable CDN for base-64. A robust base64 encoder/decoder that is fully compatible with `atob()` and `btoa()`, written in JavaScript.
Published: May 13, 2014
Top answer
1 of 8
2832

Buffers can be used for taking a string or piece of data and doing Base64 encoding of the result. For example:

> console.log(Buffer.from("Hello World").toString('base64'));
SGVsbG8gV29ybGQ=
> console.log(Buffer.from("SGVsbG8gV29ybGQ=", 'base64').toString('ascii'))
Hello World

The Buffer constructor is a global object, so no require is needed. Buffers created with strings can take an optional encoding parameter to specify what encoding the string is in. The available toString and Buffer constructor encodings are as follows:

'ascii' - for 7 bit ASCII data only. This encoding method is very fast, and will strip the high bit if set.

'utf8' - Multi byte encoded Unicode characters. Many web pages and other document formats use UTF-8.

'ucs2' - 2-bytes, little endian encoded Unicode characters. It can encode only BMP(Basic Multilingual Plane, U+0000 - U+FFFF).

'base64' - Base64 string encoding.

'binary' - A way of encoding raw binary data into strings by using only the first 8 bits of each character. This encoding method is deprecated and should be avoided in favor of Buffer objects where possible. This encoding will be removed in future versions of Node.

2 of 8
402

The accepted answer previously contained new Buffer(), which is considered a security issue in Node.js versions greater than 6 (although it seems likely for this use case that the input can always be coerced to a string).

The Buffer constructor is deprecated according to the documentation.

The code snippets should read:

console.log(Buffer.from("Hello World").toString('base64'));
console.log(Buffer.from("SGVsbG8gV29ybGQ=", 'base64').toString('ascii'));

After this answer was written, it has been updated and now matches this.

🌐
GitHub
github.com › snowplow › base64-js
GitHub - snowplow-archive/base64-js: Node.js module to handle base64 encoding arbitrary strings · GitHub
August 13, 2024 - Node.js module to handle base64 encoding arbitrary strings - snowplow-archive/base64-js
Author: snowplow-archive
🌐
GitLab
gitlab.galileo.usg.edu › galileo-gil conferences › repository
node_modules/base64-js · main · GALILEO / GALILEO-GIL Conferences · GitLab
npm install base64-js and var base64js ... supported base64-js with the Tidelift Subscription · base64js has three exposed functions, byteLength, toByteArray and fromByteArray, which both take a single argument....
🌐
GitHub
github.com › dankogai › js-base64
GitHub - dankogai/js-base64: Base64 implementation for JavaScript · GitHub
Base64 implementation for JavaScript. Contribute to dankogai/js-base64 development by creating an account on GitHub.
Author: dankogai
🌐
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 - When building an application or writing a program, you may need to encode or decode with HTML Base64 in JavaScript. This is possible thanks to two Base64 helper functions that are part of the HTML specification and are supported by all modern browser...
🌐
npm
npmjs.com › package › base64-js
base64-js - npm
November 11, 2020 - Base64 encoding/decoding in pure JS. Latest version: 1.5.1, last published: 6 years ago. Start using base64-js in your project by running `npm i base64-js`. There are 3297 other projects in the npm registry using base64-js.
      » npm install base64-js
    
Published: Nov 11, 2020
Version: 1.5.1
🌐
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 - The Buffer class in Node.js can be used to convert a string to a series of bytes. This can be done using Buffer.from() method that accepts the string to be converted and the current encoding of the string. This encoding can be specified as "utf8". The converted bytes can then be returned as a base64 using the toString() method.