1var string = "Hello folks how are you doing today?";
2var encodedString = btoa(string); // Base64 encode the String
3var decodedString = atob(encodedString); // Base64 decode the String
4
1const encoded = window.btoa('Alireza Dezfoolian'); // encode a string
2const decoded = window.atob(encoded); // decode the string
3
1str = "The quick brown fox jumps over the lazy dog";
2b64 = btoa(unescape(encodeURIComponent(str)));
3str = decodeURIComponent(escape(window.atob(b64)));
1var decode = function(input) {
2 // Replace non-url compatible chars with base64 standard chars
3 input = input
4 .replace(/-/g, '+')
5 .replace(/_/g, '/');
6
7 // Pad out with standard base64 required padding characters
8 var pad = input.length % 4;
9 if(pad) {
10 if(pad === 1) {
11 throw new Error('InvalidLengthError: Input base64url string is the wrong length to determine padding');
12 }
13 input += new Array(5-pad).join('=');
14 }
15
16 return input;
17 }
18