Encode string to Base64

OSCC · 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'

Recommended snippets