Online Base64 Encode Decode
The atob()
and btoa()
functions in JavaScript are used to encode and decode strings using Base64 encoding. Base64 encoding is a way of representing binary data as ASCII text, which is useful for transmitting or storing binary data in a text-based format.
1. btoa()
: The btoa()
function is used to encode a string to Base64 format.
javascript
const encodedString = btoa('Hello, World!');
console.log(encodedString);
Output: SGVsbG8sIFdvcmxkIQ==
In the example above, the btoa()
function encodes the string 'Hello, World!'
to Base64 format. The result is stored in the encodedString
variable, and it is printed to the console.
2. atob()
: The atob()
function is used to decode a Base64-encoded string back to its original form.
javascript
const decodedString = atob('SGVsbG8sIFdvcmxkIQ==');
console.log(decodedString);
Output: Hello, World!
In this example, the atob()
function decodes the Base64-encoded string 'SGVsbG8sIFdvcmxkIQ=='
back to its original form. The result is stored in the decodedString
variable and printed to the console.
It's important to note that these functions are available in modern browsers and in Node.js for server-side JavaScript. However, they may not be available in all environments, so it's a good practice to check for their existence before using them. You can use the following code to check if the functions are available:
javascript
if (typeof btoa === 'function' && typeof atob === 'function')
For more information on atob()
and btoa()
, you can refer to the Mozilla Developer Network (MDN) documentation: