This is an issue when using String#charCodeAt() or String.fromCharCode() methods, which don’t properly handle Unicode characters that require surrogate pairs.

Why is this an issue?

JavaScript strings use UTF-16 encoding internally. Some Unicode characters, like emojis and certain international characters, require two 16-bit code units (called surrogate pairs) to represent a single character.

The older methods charCodeAt() and fromCharCode() work with individual UTF-16 code units. When dealing with characters that use surrogate pairs, these methods only process the first code unit, leading to incorrect results.

For example, the emoji '🦄' (unicorn) has the Unicode code point U+1F984. In UTF-16, this becomes a surrogate pair. Using charCodeAt(0) only returns the first part of this pair, not the complete character code.

The newer methods codePointAt() and fromCodePoint() understand Unicode code points properly. They handle surrogate pairs correctly and return the actual Unicode code point value, ensuring your application works correctly with international text and emojis.

What is the potential impact?

Using the wrong methods can cause:

How to fix?

Replace charCodeAt() with codePointAt() to get the correct Unicode code point value.

Non-compliant code example

const code = '🦄'.charCodeAt(0); // Noncompliant

Compliant code example

const code = '🦄'.codePointAt(0);

Documentation