What is JS Base64 encode?
btoa() to encode data to Base64 format.
How to use btoa() function?
btoa() function takes a string as input and returns a Base64 encoded string. Here's an example:
let originalString = "Hello, World!"; let encodedString = btoa(originalString); console.log(encodedString); // Output: "SGVsbG8sIFdvcmxkIQ=="
The encoded string can now be safely transmitted over the internet without the risk of data loss or corruption. To decode the string back into its original form, use the
Alternative method: Using a third-party library
Here's an example using js-base64:
let originalString = "Hello, World!"; let encodedString = Base64.encode(originalString); console.log(encodedString); // Output: "SGVsbG8sIFdvcmxkIQ=="
In addition to encoding and decoding strings, js-base64 also supports encoding and decoding binary data, URL-safe encoding, and decoding, and more.