w3resource

JavaScript: Create a base-64 encoded ASCII string from a String object in which each character in the string is treated as a byte of binary data

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

Encode String to Base-64

Write a JavaScript program to create a base-64 encoded ASCII string from a String object in which each character in the string is treated as a byte of binary data.

  • Create a Buffer for the given string with binary encoding and use Buffer.toString('base64') to return the encoded string.

Sample Solution:

JavaScript Code:

//#Source https://bit.ly/2neWfJ2 
// Define a function 'btoa' to encode a string to base64
const btoa = str => new Buffer(str, 'binary').toString('base64');

// Encode the string 'foobar' to base64
console.log(btoa('foobar'));

Flowchart:

flowchart: Create a base-64 encoded ASCII string from a String object in which each character in the string is treated as a byte of binary data.

Improve this sample solution and post your code through Disqus

Previous: Write a JavaScript program that will return true if the bottom of the page is visible, false otherwise.
Next: Write a JavaScript program to capitalize the first letter of a string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/javascript-exercises/fundamental/javascript-fundamental-exercise-262.php