yarek
yarek

Reputation: 12064

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 !

Upvotes: 9

Views: 15903

Answers (2)

mohsen
mohsen

Reputation: 1

Use this:

// emoji code from w3schools //
//***************************//
  var code = {
    ok: "9989",     hurrcane : "128165",  Up : "11014",    Down : "11015",    Hashtag : "35",    Clock : "9200", P : "127359",    Okay : "10004",    NotOkay : "10006"
  }
// emoji in javascript       //
//***************************//
  function emoji(ecode)
  {
    return String.fromCodePoint(ecode);
  }
  console.log(emoji(code.Clock)+emoji(code.Okay))

Upvotes: -1

Hugues M.
Hugues M.

Reputation: 20497

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.

Upvotes: 19

Related Questions