Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between codePointAt and charCodeAt

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

'A'.codePointAt(); // 65 'A'.charCodeAt();  // 65 
like image 572
Stanislav Mayorov Avatar asked Apr 10 '16 08:04

Stanislav Mayorov


People also ask

What is the difference between charCodeAt and codePointAt in Javascript?

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).

What is charCodeAt?

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.

What is the use of charCodeAt ()?

The charCodeAt() method returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index.

What is the difference between Charat () and charcodeat () methods?

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.

What is charcodeat in Unicode character encoding?

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).

Is codepointat() Unicode or hex?

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:

What is codepointat() method?

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, ....


1 Answers

From the MDN page on charCodeAt:

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 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, use codePointAt().

TLDR;

  • charCodeAt() is UTF-16
  • codePointAt() is Unicode.
like image 177
ToxicTeacakes Avatar answered Oct 06 '22 01:10

ToxicTeacakes