Search code examples
javascriptunicode

How to insert emoticons chars into JS script


How can I use emoticon chars in JS :

http://unicode.org/emoji/charts/full-emoji-list.html#1f600

I tried with:

var emoji = String.fromCharCode(0x1F621);
var emoji = '\u1F600';

I also tried to copy/paste them into phpStorm IDE and into sublime text : that gave me a squared missing char.

I just need to have a console.log('😡') inside my JS !


Solution

  • var emoji = String.fromCodePoint(0x1F621)
    

    Result:

    "😡"
    

    Be careful about limited browser support, though, as String.fromCodePoint() is part of the ES2015 standard. See Mozilla's polyfill if needed.

    The language spec explains why fromCharCode does not work:

    [the argument is] converted to a character by applying the operation ToUint16 and regarding the resulting 16-bit integer as the code unit value of a character.

    and the emoji block is beyond the maximum value supported by unsigned 16-bit integers.