Node.js - atob / btoa functions equivalents

1 points
Created by:

btoa() and atob() are common JavaScript functions, but are not included in Node.js. Below you will find the equivalents of these functions in Node.js.


In the presented solution, we use the Buffer class, which is in a global scope in Node.js and does not need to be imported using the require.

btoa function equivalent

// "btoa" should be read as "binary to ASCII"
// btoa converts binary to Base64-encoded ASCII string
const btoa = (text) => {
    return Buffer.from(text, 'binary').toString('base64');

console.log(btoa('This is text...')); // VGhpcyBpcyB0ZXh0Li4u

atob function equivalent

// "atob" should be read as "ASCII to binary"
// atob converts Base64-encoded ASCII string to binary
const atob = (base64) => {
    return Buffer.from(base64, 'base64').toString('binary');

console.log(atob('VGhpcyBpcyB0ZXh0Li4u')); // This is text...

See also:

  1. Node.js - base64 with Unicode support
  2. Node.js - encoding / decoding base64

Alternative titles

  1. atob function in Node.js
  2. btoa function in Node.js
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Node.js - base64

Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂