The JavaScript string.codePointAt() method
The codePointAt() method returns the full Unicode code point of the character at an index — correctly handling emoji and other characters outside the basic range, which charCodeAt() can't. Its inverse is String.fromCodePoint().
Overview
codePointAt() is the Unicode-aware version of charCodeAt(). The problem it solves: JavaScript strings are stored as UTF-16 code units, and characters above the basic plane — emoji, many CJK and historic scripts — are stored as two units (a surrogate pair). charCodeAt() only sees one half of such a character and returns a meaningless surrogate value. codePointAt() reads the whole thing and returns the real code point.
For ordinary characters (Latin letters, digits, most punctuation) the two methods agree. The difference only shows up with astral characters, but when it does, codePointAt() is the correct choice. Its inverse is the static String.fromCodePoint(), which turns a code point back into a character — the reliable pair for round-tripping any Unicode character.
This connects to a broader point about strings and Unicode: methods that work per code unit (like indexing or split("")) can break emoji apart, while iterating a string with for...of or spreading it ([...str]) walks by code point and keeps them whole. When correctness with emoji matters, prefer the code-point-aware tools.
Syntax
str.codePointAt(index)
"A".codePointAt(0) // 65
"a".codePointAt(0) // 97
// an emoji is one code point but two UTF-16 units
String.fromCodePoint(128512) // a smiley
Parameters
The string.codePointAt() method accepts the following parameters.
| Parameter | Description |
|---|---|
index |
The position (in UTF-16 code units) of the character whose code point you want. |
Example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
const s = 'ABC';
const points = [...s].map(ch => ch + '=' + ch.codePointAt(0));
document.getElementById('out').textContent =
points.join(', ') + '\n' +
'fromCodePoint(72,73): ' + String.fromCodePoint(72, 73);
// A=65, B=66, C=67 / fromCodePoint(72,73): HI
</script>
Best practices
- Use
codePointAt()over charCodeAt() when emoji or astral characters are possible. - Pair it with
String.fromCodePoint()to convert code points back to characters. - Iterate strings with for...of or spread (
[...str]) to keep emoji whole. - For plain ASCII/Latin text, charCodeAt() is fine and equivalent.
Frequently asked questions
What is the difference between codePointAt() and charCodeAt()?
codePointAt() returns the full Unicode code point (handling emoji and astral characters); charCodeAt() returns a single 16-bit code unit, which only sees half of such characters.How do I get the code point of an emoji?
emoji.codePointAt(0). charCodeAt() would return a surrogate value, not the real code point.How do I convert a code point back to a character?
String.fromCodePoint(codePoint) — the inverse of codePointAt().