Can you use String.fromCodePoint in the same way as String.fromCharCode

I am still learning JavaScript and practicing getting keyboard input. I just found out about String.fromCodePoint and it seems (to me) to pick up everything that String.fromCharCode does.

Is String.fromCodePoint supported String.fromCodePoint browsers and devices, and if so, does it String.fromCharCode obsolete, or is there a reason you would sometimes use String.fromCharCode ?

+5
source share
3 answers

fromCharCode is not outdated yet, but it will be supported by all browsers. However, fromCharCode about two times faster than fromCodePoint

  • String.fromCodePoint () Not Supported by Internet Explorer and Safari

  • String.fromCharCode () Supported forever, twice as fast

  • Difference:

    Although the most common Unicode values ​​can be represented by a single 16-bit number (as expected early in the standardization of JavaScript), and fromCharCode () can be used to return a single character for the most common values ​​(i.e. UCS-2 values ​​that are a subset of UTF -16 with the most common characters), in order to deal with ALL legitimate Unicode values ​​(up to 21 bits), fromCharCode () alone is inadequate. Since higher code point characters use two (lower values) surrogate numbers to form one character, String.fromCodePoint () (part of the ES6 draft) can be used to return such a pair, and thus adequately represent these higher-value characters.

+4
source

String.fromCodePoint not widely supported, in fact it is not supported at all in Internet Explorer and Safari, and only in Chrome 41 and higher, and Firefox 29 and higher.

This does not mean that it is out of date, it means that this is a new method, only the first one defined in ES2015.
It also means that browser support will improve over time as browsers implement all the new features in the 6th edition of ECMAScript.

Currently, it is not very suitable for use in production if you need to support all current browsers, but there is a polyfill available on MDN if you really need this method in all browsers.

+4
source

String.fromCharCode is supported in all browsers:
enter image description here

String.fromCodePoint has limited browser support:
enter image description here

+3
source

Source: https://habr.com/ru/post/1240178/


All Articles