2

When I try to use a string template to generate an emoji I get an error.

How to achieve this purpose?

Javascript code:

const unifiedValue = '1F60E';
const emoji = `\u{${ unifiedValue }}`;

Javascript error:

SyntaxError: Invalid escape sequence in template

Of course if I use the value directly it works, but that would be error-prone + thousands lines of code with hundreds of if conditions.

Developer console:

'\u{1F60E}'

Console Output: ""

Krzysztof Janiszewski
  • 3,763
  • 3
  • 18
  • 38
sertsedat
  • 3,490
  • 1
  • 25
  • 45

2 Answers2

10

Use fromCodePoint function.

Here is a wroking solution

const unifiedValue = '1F60E';

var emoji = `0x${unifiedValue}`;
emoji = String.fromCodePoint(emoji);

console.log(emoji);

Also keep in mind that our "beloved" internet explorer does not support it :/

enter image description here

EDIT

const unifiedValue = '1F60E';

var emoji = parseInt(unifiedValue, 16);

emoji = String.fromCodePoint(emoji);

console.log(emoji);
Krzysztof Janiszewski
  • 3,763
  • 3
  • 18
  • 38
0

I think the problem here is you used template quote. You must use double backslash in template quote.

const emoji = `\u{${ unifiedValue }}`;

Instead of this

const emoji = `\\u{${ unifiedValue }}`;
cabrakan
  • 1
  • 2