0

The problem is that the String.fromCharCode() method return an incorrect match (or has some limitation (not sure which exactly)

I have tried some of the char codes from here. And all of the items under 12953 are working fine. But if you try anything, starting from 126980 it stops working and displays .

However, if I try checking it here, it finds it.

Is it because something is missing? or it could be because of "surrogate pair"(the characters that have more than one code unit)?

By running the following lines of code:

const emojis = [12953, 126980]; // ㊙, 

console.log(String.fromCharCode(emojis[0])); // displayed correctly as it is ㊙
console.log(String.fromCharCode(emojis[1])); //  is dispalyed instead of 
Utmost Creator
  • 814
  • 1
  • 9
  • 21
  • 2
    According to the documentation, [String.fromCharCode(n) requires 0 <= n <= 65535](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode). The function you want is `String.fromCodePoint`. – Raymond Chen Feb 09 '22 at 00:59
  • Oh, thank you so much, I have been working the whole day, and probably I am tired xD. I knew that there are `codePointAt()`, `charCodeAt()`, but only after fully reading the documentation i have found the similar property/method called `String.fromCodePoint()`. again thank you @raymond-chen – Utmost Creator Feb 09 '22 at 01:05
  • indirectly, but yes, it touches the things I needed. thank you! Does this mean that I always need to use `String.fromCodePoint()` over `String.fromCharCode()`? Could you please answer this: "why if I try to use `codePointAt(1)` on single emoji, it returns me lower surrogate pair"? – Utmost Creator Feb 09 '22 at 01:25
  • 1
    The emoji symbol is represented by two characters, one at offset 0 (the high surrogate) and another at offset 1 (the low surrogate). If you ask fromCodePoint to start at offset 1, then all it sees is the low surrogate. Read up on UTF-16 encoding to understand how this works and why breaking up surrogates produces the results you observe. – Raymond Chen Feb 09 '22 at 16:41

1 Answers1

0

After fully reading the docs page, I have found

And there are methods to go from one state to another for some characters (e.g. emojis): charCodeAt() and codePointAt() are used to get the hexadecimal values, and the latter one is used for "surrogate pair" or the values that go outside of the 65535 (216) range more about these two here.

Quote from here

For this reason, it's more convenient to use String.fromCodePoint() (part of the ES2015 standard), which allows for returning supplementary characters based on their actual code point value. For example, String.fromCodePoint(0x1F303) returns code point U+1F303 "Night with Stars".

This is exactly what i need String.fromCodePoint(). That allows me to convert the hexadecimal value into emoji.

and here is the final code:

const emojis = [12953, 126980]; // ㊙, 

console.log(String.fromCodePoint(emojis[0])); // displayed correctly as it is ㊙
console.log(String.fromCodePoint(emojis[1])); // displayed correctly as it is 
Utmost Creator
  • 814
  • 1
  • 9
  • 21