0

I was reviewing a code where they author did something like this

const emojis: Array<Icon | undefined> | undefined = emojiSet
  .map((emoji) => {
    if (emoji.name && emoji.unified) {
      const codePoints = emoji.unified
        .split("-")
        .map((token) => Number("0x" + token));
        
      return {
        name: emoji.name,
        value: String.fromCodePoint(...codePoints),
        index: emoji.unified,
      };
    }
    return undefined;
  })
  .filter(Boolean);

To understand the code, I went through this repo (which is what the above is using to iterate over the json)

Here, in the example unified is "unified": "261D-FE0F",

When he does this,

   const codePoints = emoji.unified
        .split("-")
        .map((token) => Number("0x" + token));

He is iterating over 261D-FE0F and mapping it to number .map((token) => Number("0x" + token));

[Question] Here I am unable to comprehend that why is he adding "Ox" to the number? how can we determine using unicode that this is going to be emoji?

THis is unicdoe blocks from wiki (if it associates with my question in anyway)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Alwaysblue
  • 9,948
  • 38
  • 121
  • 210
  • 1
    The `0x` prefix is used to denote a hexadecimal number. Compare: `console.log(0x145, 145)`. – Felix Kling Aug 27 '20 at 13:19
  • Does this answer your question? [Why are hexadecimal numbers prefixed with 0x?](https://stackoverflow.com/questions/2670639/why-are-hexadecimal-numbers-prefixed-with-0x) – evolutionxbox Aug 27 '20 at 13:20

1 Answers1

2

Because the numbers are in hex. 0x is a prefix used to indicate hex numbers. Number("261D") is NaN, but Number("0x261D") is 9757.

Other prefixes available in JavaScript are 0b (binary, e.g. 0b11 is 3) and 0o (octal, e.g. 0o11 is 9).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875