base 64 in javascript

To encode text or data into Base64 in JavaScript, the btoa() method can be used. This method converts a string of data to a Base64-encoded ASCII string.

Here's an example of how to use btoa():

index.tsx
let data = "Hello, world!";
let encodedData = btoa(data);
console.log(encodedData); // "SGVsbG8sIHdvcmxkIQ=="
110 chars
4 lines

To decode a Base64-encoded string back into the original data or text, the atob() method can be used.

Here's an example of how to use atob():

index.tsx
let encodedData = "SGVsbG8sIHdvcmxkIQ==";
let decodedData = atob(encodedData);
console.log(decodedData); // "Hello, world!"
124 chars
4 lines

related categories

gistlibby LogSnag