What is the difference between String.prototype.codePointAt()
and String.prototype.charCodeAt()
in JavaScript?
'A'.codePointAt(); // 65 'A'.charCodeAt(); // 65
Difference Between charCodeAt() and codePointAt()charCodeAt() is UTF-16, codePointAt() is Unicode. charCodeAt() returns a number between 0 and 65535. Both methods return an integer representing the UTF-16 code of a character, but only codePointAt() can return the full value of a Unicode value greather 0xFFFF (65535).
The charCodeAt() method returns the Unicode of the character at a specified index (position) in a string. The index of the first character is 0, the second is 1, .... The index of the last character is string length - 1 (See Examples below). See also the charAt() method.
The charCodeAt() method returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index.
The String object's charAt () method returns a new string consisting of the single UTF-16 code unit located at the specified offset into the string. The charCodeAt () method returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index.
In the Unicode character encoding, characters are mapped to values between 0x0 and 0x10FFFF. read more Unicode - It assigns every character a unique number called a code point. charCodeAt (pos) returns code a code unit (not a full character).
codePointAt () is Unicode. Just to add on, many unicode tables show hex values, e.g. U+00A0. Since codePointAt returns an int, you may want to convert it to hex by calling .toString (16) if you are looking up the unicode table manually. To add a few for the ToxicTeacakes's answer, here is another example to help you know the difference:
More examples below. The codePointAt () method returns the Unicode value at an index (position) in a string. The index of the first position is 0, the second is 1, ....
From the MDN page on charCodeAt
:
The
charCodeAt()
method returns an integer between0
and65535
representing the UTF-16 code unit at the given index.The UTF-16 code unit matches the Unicode code point for code points which can be represented in a single UTF-16 code unit. If the Unicode code point cannot be represented in a single UTF-16 code unit (because its value is greater than
0xFFFF
) then the code unit returned will be the first part of a surrogate pair for the code point. If you want the entire code point value, usecodePointAt()
.
charCodeAt()
is UTF-16 codePointAt()
is Unicode.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With