w3resource

JavaScript: Decode a string of data which has been encoded using base-64 encoding

JavaScript fundamental (ES6 Syntax): Exercise-259 with Solution

Write a JavaScript program to decode a string of data which has been encoded using base-64 encoding.

Note: Create a Buffer for the given string with base-64 encoding and use Buffer.toString('binary') to return the decoded string.

  • Create a Buffer for the given string with base-64 encoding and use Buffer.toString('binary') to return the decoded string.

Sample Solution:

JavaScript Code:

//#Source https://bit.ly/2neWfJ2 
const atob = str => new Buffer(str, 'base64').toString('binary');
console.log(atob('Zm9vYmFy'));

Flowchart:

flowchart: Decode a string of data which has been encoded using base-64 encoding.

Improve this sample solution and post your code through Disqus

Previous: Write a JavaScript program to create a function that accepts up to n arguments, ignoring any additional arguments.
Next: Write a JavaScript program to evaluate the binomial coefficient of two integers n and k.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



JavaScript: Tips of the Day

How to check whether a string contains a substring in JavaScript?

ECMAScript 6 introduced String.prototype.includes:

const string = "foo";
const substring = "oo";

console.log(string.includes(substring));

includes doesn't have Internet Explorer support, though. In ECMAScript 5 or older environments, use String.prototype.indexOf, which returns -1 when a substring cannot be found:

var string = "foo";
var substring = "oo";

console.log(string.indexOf(substring) !== -1);

Ref: https://bit.ly/3fFFgZv