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.](https://www.w3resource.com/w3r_images/javascript-basic-exercise-flowchart-262-1.png)
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.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics