Here's an example of how to encode and decode data using Base64 in JavaScript:
Encoding:
// string to be encoded let stringToEncode = "Hello, World!"; // encode the string using Base64 let encodedString = btoa(stringToEncode); console.log(encodedString); // "SGVsbG8sIFdvcmxkIQ=="
In the above example, we use the btoa() function to encode the string, and atob() function to decode the string. Note that btoa() and atob() functions are not available in all browsers, so you may need to use a polyfill or a library like base64-js to ensure cross-browser compatibility.
Also, keep in mind that while Base64 encoding is useful for encoding data to be sent over the network, it is not a secure form of encryption and should not be used for storing sensitive data.
No comments:
Post a Comment