Encode string to Base64

JavaScript, Node, String · Sep 15, 2020

Creates 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.
  • Use Buffer.prototype.toString() to return the base-64 encoded string.
const btoa = str => Buffer.from(str, 'binary').toString('base64');
btoa('foobar'); // 'Zm9vYmFy'

More like this

  • JavaScript String Snippets

    Master string manipulation in JavaScript with this ES6 snippet collection.

    Collection · 108 snippets

  • Decode Base64 encoded string

    Decodes a string of data which has been encoded using base-64 encoding.

    JavaScript, Node · Sep 15, 2020

  • JSON to file

    Writes a JSON object to a file.

    JavaScript, Node · Oct 20, 2020

  • Colorize text

    Adds special characters to text to print in color in the console (combined with console.log()).

    JavaScript, Node · Nov 3, 2020