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
๐ŸŒ
W3docs
w3docs.com โ€บ home โ€บ code snippets โ€บ javascript โ€บ how to encode and decode strings with base64 in javascript
How to Encode and Decode Strings with Base64 in JavaScript | W3docs
There is a counterpart function, <kbd class="highlighted">atob()</kbd>, to convert from Base64 encoding back to a string. The <kbd class="highlighted">atob()</kbd> function returns a string where each character represents an 8-bit byte, meaning ...
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

๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Glossary โ€บ Base64
Base64 - Glossary - MDN Web Docs - Mozilla
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").
๐ŸŒ
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 - The resulting binary data is a reconstruction of the original binary data encoded to Base64. To encode and decode in JavaScript, you will use the btoa() and atob() JavaScript functions that are available and supported by modern web browsers.
๐ŸŒ
CoreUI
coreui.io โ€บ answers โ€บ how-to-decode-a-base64-string-in-javascript
How to decode a base64 string in JavaScript ยท CoreUI
May 21, 2026 - This approach is efficient, widely supported, and specifically designed for ASCII-to-binary conversion in web environments. Use the atob() function to decode a base64-encoded string back to its original format.
๐ŸŒ
Base64 Decode
base64decode.org
Base64 Decode and Encode - Online
Decode each line separately: The encoded data usually consists of continuous text, so even newline characters are converted into 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, without sending any information to our servers.
Find elsewhere
๐ŸŒ
Javatpoint
javatpoint.com โ€บ base64-decoding-in-javascript
Base64 Decoding In JavaScript - javatpoint
March 27, 2023 - Base64 Decoding In JavaScript with javascript tutorial, introduction, javascript oops, application of javascript, loop, variable, objects, map, typedarray etc.
๐ŸŒ
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 ...
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Uint8Array โ€บ fromBase64
Uint8Array.fromBase64() - JavaScript - MDN Web Docs
The last chunk must be exactly 4 characters long with padding = characters. Furthermore, overflow bits (trailing bits from the last base64 character that don't represent any data) must be 0. The last chunk is decoded and appended to the result.
๐ŸŒ
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. atob should produce the original string.
๐ŸŒ
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 - 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:
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ API โ€บ Window โ€บ atob
Window: atob() method - Web APIs | MDN
The atob() method of the Window interface decodes a string of data which has been encoded using Base64 encoding. You can use the Window.btoa() method to encode and transmit data which may otherwise cause communication problems, then transmit it and use the atob() method to decode the data again.
๐ŸŒ
Programiz
programiz.com โ€บ javascript โ€บ examples โ€บ encode-string-Base64
JavaScript Program to Encode a String to Base64
// program to encode a string to Base64 // defining the string const str = "Learning JavaScript"; // encoding the string const result = window.btoa(str); console.log(result); // decoding the string const result1 = window.atob(result); console.log(result1);