The Node btoa() and atob() functions are the only ones that have been deprecated. However, if you're working on the DOM code (front-end) and see this deprecated notice, you can use the window object to get around it.
window.atob()
For more info
Answer from Kamal Thennakoon on Stack OverflowWhat does **b** and **a** in `btoa()` mean?
How to use "btoa" (and "atob") in Javascript? - Feature Suggestions - Fuse Community
Using `btoa` and `atob` for faster Base64 encoding and decoding
Function - how to do btoa javascript function
The Node btoa() and atob() functions are the only ones that have been deprecated. However, if you're working on the DOM code (front-end) and see this deprecated notice, you can use the window object to get around it.
window.atob()
For more info
TLDR: These functions btoa and atob are deprecated for Node JS. If you are coding for the web browser you just need to prepend the window to get rid of this deprecation mark.
to encode :
window.btoa('test')
result: dGVzdA==
to decode
window.atob('dGVzdA==')
result: test
Otherwise, you could install buffer with "yarn add buffer" or "npm i buffer" to run on browser. The buffer module's API is identical to node's Buffer API.
Here is a React sample using javascript module to browser, but it can run on any modern javascript frontend app that uses webpack or parcel or even vanilla javascript with script src:
//this will run on browser
import React from "react";
import { Buffer } from 'buffer';
export default function App() {
const encodeBase64 = (data) => {
return Buffer.from(data).toString('base64');
}
const decodeBase64 = (data) => {
return Buffer.from(data, 'base64').toString('ascii');
}
return <div>
{'encoded test to base64 = ' + encodeBase64('test')}<br />
{'decoded dGVzdA== to ascII = ' + decodeBase64('dGVzdA==')}
</div>;
}
The above react hook will result:
encoded test to base64 = dGVzdA==
decoded dGVzdA== to ascII = test
btoa(): accepts a string where each character represents an 8bit byte. If you pass a string containing characters that cannot be represented in 8 bits, it will probably break. Probably that's why btoa is deprecated.
atob(): returns a string where each character represents an 8-bit byte.
If you are using nodejs just replace atob and btoa with Buffer. Here is the official documentation Buffers in nodejs:
//With NodeJS
export const encodeBase64 = (data) => {
return Buffer.from(data).toString('base64');
}
export const decodeBase64 = (data) => {
return Buffer.from(data, 'base64').toString('ascii');
}
There are two functions called btoa() and atob() which will respectively encode strings to base64 and decode them from it.
But what does a/b stand for? I tried to remember them as btoa, will convert base to something and atob will convert something to base and worked fine for me until now. I was just wondering what actually are them?