charCodeAt vs codePointAt
JavaScript stores strings as UTF-16, which means a sequence of 16-bit code units. Most characters fit in one code unit, but characters beyond U+FFFF, such as most emoji, take two, called a surrogate pair. charCodeAt() reads a single code unit. codePointAt() reads a whole Unicode code point, combining a surrogate pair into one number. The difference only shows with characters that need two code units, but those are common enough that codePointAt() is the safer choice.
Characters that fit in one code unit
Section titled “Characters that fit in one code unit”For ordinary letters, digits and symbols, both methods return the same number. toString(16) shows it in hexadecimal, the form used in Unicode charts.
const price = '£5';
console.log(price.charCodeAt(0), price.codePointAt(0));console.log(price.charCodeAt(0).toString(16));console.log(price.charCodeAt(1), price.charCodeAt(9));This prints:
163 163a353 NaNThe pound sign is U+00A3, and the digit 5 is 53. A position past the end gives NaN from charCodeAt(), while codePointAt() returns undefined.
Characters that take two code units
Section titled “Characters that take two code units”The party emoji is U+1F389, which is too large for one code unit. It occupies positions 3 and 4 of the string, and length counts both.
const greeting = 'Hi 🎉';
console.log(greeting.length);console.log(greeting.charCodeAt(3), greeting.charCodeAt(4));console.log(greeting.codePointAt(3), greeting.codePointAt(3).toString(16));console.log(greeting.codePointAt(4));This prints:
555356 57225127881 1f38957225charCodeAt() returns the two halves of the pair separately, neither of which is a character on its own. codePointAt(3) returns the real code point. Positions still count code units, though, so codePointAt(4) lands on the second half and returns it as it is.
Building strings from codes
Section titled “Building strings from codes”String.fromCharCode() builds a string from code units, and String.fromCodePoint() builds one from code points. Both take any number of arguments.
console.log(String.fromCharCode(72, 105));console.log(String.fromCodePoint(0x1f389));console.log(String.fromCharCode(0xd83c, 0xdf89));console.log(String.fromCodePoint(72, 105, 0x1f389));This prints:
Hi🎉🎉Hi🎉fromCharCode() can only make the emoji when given both halves of the surrogate pair, while fromCodePoint() takes the code point directly. A code point above U+FFFF passed to fromCharCode() is silently cut down to 16 bits, which gives the wrong character, so use fromCodePoint() for anything that might be an emoji.
Looping by code point
Section titled “Looping by code point”A for...of loop steps through a string one code point at a time, so it never splits a surrogate pair.
for (const ch of 'a£🎉') { const hex = ch.codePointAt(0).toString(16).toUpperCase().padStart(4, '0'); console.log(ch, `U+${hex}`, ch.length);}This prints:
a U+0061 1£ U+00A3 1🎉 U+1F389 2Code points are not always characters
Section titled “Code points are not always characters”What a reader sees as one character can be several code points. An emoji with a skin tone is the base emoji followed by a modifier, and flags and many accented letters are built the same way. Intl.Segmenter splits text into these user-perceived characters, which Unicode calls grapheme clusters.
const wave = '👋🏽';const segmenter = new Intl.Segmenter('en', { granularity: 'grapheme' });
console.log(wave.length);console.log([...wave].length);console.log([...segmenter.segment(wave)].length);This prints:
421The wave is four code units, two code points and one visible character. Use Intl.Segmenter when counting or cutting text for display, for example when truncating a user’s name. Strings as arrays shows the same issue when splitting a string.
Steve Griffith covers this topic in his video Understanding charCodeAt vs codePointAt.