js to base 64 code example
Example 1: javascript base64 encode
var string = "Hello folks how are you doing today?";
var encodedString = btoa(string);
var decodedString = atob(encodedString);
Example 2: base 64 in js
var string = 'Hello World!';
var encodedString = btoa(string);
console.log(encodedString);
var decodedString = atob(encodedString);
console.log(decodedString);
Example 3: javascript base64 decode
var decodedString = atob(encodedString);
Example 4: javascript base64url decode
var decode = function(input) {
input = input
.replace(/-/g, '+')
.replace(/_/g, '/');
var pad = input.length % 4;
if(pad) {
if(pad === 1) {
throw new Error('InvalidLengthError: Input base64url string is the wrong length to determine padding');
}
input += new Array(5-pad).join('=');
}
return input;
}