Skip to main content

Convert emoji character to Unicode number in JavaScript

· One min read
Leibniz Li

String.prototype.codePointAt

The codePointAt method returns a non-negative integer that is the Unicode code point value at the given position.

'☃★♲'.codePointAt(0);
// output: "9731"
'☃★♲'.codePointAt(1);
// output: "9733"
'☃★♲'.codePointAt(2);
// output: "9842"

"😀".codePointAt(0);
// output: "128512"
"😀".codePointAt(1);
// output: "56832"
"😀".codePointAt(0).toString(16);
// output: "1f600"
'😀'.length
// output: "2"

"🇺🇸".codePointAt(0);
// output: "127482"
"🇺🇸".codePointAt(1);
// output: "56826"
"🇺🇸".codePointAt(2);
// output: "127480"
"🇺🇸".codePointAt(3);
// output: "56824"
'🇺🇸'.length
// output: "4"

String.fromCodePoint

The static String.fromCodePoint method returns a string created by using the specified sequence of code points.

String.fromCodePoint(9731, 9733, 9842, 0x2F804);
// output: "☃★♲你"

String.fromCodePoint(128512, 56832);
// output: "😀\uDE00"

String.fromCodePoint(128512);
// output: "😀"

String.fromCodePoint("0x"+"1f600");
// output: "😀"

String.fromCodePoint(127482,127480);
// output: "🇺🇸"
Unicode encode/Unicode decode/Emoji encode/Emoji decode