How to base64 encode emojis in JavaScript?

You can encode it by escaping it first and then calling EncodeUriComponent on it.

This looks like this:

btoa(unescape(encodeURIComponent('')));

The emoji above would return "8J+Ygg=="

To decode it you would do this

decodeURIComponent(escape(window.atob('8J+Ygg==')));

You could make two functions that make this a bit easier:

//Encode
function utoa(str) {
    return window.btoa(unescape(encodeURIComponent(str)));
}
//Decode
function atou(str) {
    return decodeURIComponent(escape(window.atob(str)));
}

Source: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/btoa