Character code
suggest changecharCodeAt returns the UTF-16 code unit at an index, not a full Unicode code point:
const charCode = "µ".charCodeAt(); // The character code of the letter µ is 181
To get the character code of a character in a string, the 0-based position of the character is passed as a parameter to charCodeAt:
const charCode = "ABCDE".charCodeAt(3); // The character code of "D" is 68
Some Unicode characters do not fit in one UTF-16 code unit. Code points above 65535 (2 ** 16 - 1) are encoded as a surrogate pair: two code units. charCodeAt returns only one unit of that pair. codePointAt returns the full code point:
// The Grinning Face Emoji has code point 128512 or 0x1F600
const codePoint = "😀".codePointAt();
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents