Difference between codePointAt and charCodeAt

2019-04-18 12:01发布

What is the difference between String.prototype.codePointAt() and String.prototype.charCodeAt() in JavaScript?

'A'.codePointAt(); // 65
'A'.charCodeAt();  // 65

3条回答
Evening l夕情丶
2楼-- · 2019-04-18 12:38

From Mozilla:

The charCodeAt() method returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index (the UTF-16 code unit matches the Unicode code point for code points representable in a single UTF-16 code unit, but might also be the first code unit of a surrogate pair for code points not representable in a single UTF-16 code unit, e.g. Unicode code points > 0x10000). If you want the entire code point value, use codePointAt().

charCodeAt() is UTF-16, codePointAt() is Unicode.

查看更多
虎瘦雄心在
3楼-- · 2019-04-18 12:39

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt

from this url, you can get the differences, their function is almost the same, but some differences on the returns and illegal argument

查看更多
Anthone
4楼-- · 2019-04-18 12:56

To add a few for the ToxicTeacakes's answer, here is another example to help you know the difference:

"                                                                    
查看更多
登录 后发表回答