Ankh
Ankh

Reputation: 31

Turn emoji into Javascript Encoding?

Is there any function or something that I can use to convert an emoji or something into the Javascript Encoding equivalent? For example, if I have a variable that contains "👍", I want to be able to convert it to its corresponding javascript encoding, like "\ud83d\udc4d". Any way this can be accomplished?

Upvotes: 2

Views: 2269

Answers (2)

pooja shinde
pooja shinde

Reputation: 1

function emojiToUnicodeEscape(emoji) {
    return emoji
        .split('')
        .map(char => "\\u" + char.charCodeAt(0).toString(16))
        .join('');
}

const myEmoji = "😀";
const unicodeEscapeSequence = emojiToUnicodeEscape(myEmoji);
console.log(unicodeEscapeSequence);

Upvotes: 0

Rajan Lagah
Rajan Lagah

Reputation: 2528

You can use punycode library.npm i punycode or visit https://www.npmjs.com/package/punycode
for example.

   console.log(punycode.ucs2.decode('👍')); // to get corresponding like "\ud83d\udc4d"code

and

console.log('\ud83d\udc4d')

to get corresponding image/emoji;

You can visit http://speakingjs.com/es5/ch24.html and https://flaviocopes.com/javascript-unicode/ for more information.


**Update**

enter image description here

You can have refrence from

Here here is the example.

<p>I will display &#9786;</p>

<p>I will display &#9786;</p>

Upvotes: 1

Related Questions