Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

String.prototype.charCodeAt — UTF-16 code unit value

The charCodeAt method returns the numeric value of the UTF-16 code unit at an index, or NaN outside the string. It does not always return a complete Unicode code point.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

string.charCodeAt(index)

String.prototype.charCodeAt reads one UTF-16 code unit. Indexes start at 0. For an in-range index, the result is a Number from 0 to 65535. The method does not modify the string.

The index argument is converted to a number, with any fractional part truncated toward zero. An omitted argument, undefined, and NaN mean index 0. The method checks the bounds only after the conversion succeeds. If the resulting index is out of range, as with a negative integer or infinity, the method returns NaN. For an empty string, every resulting index is out of range, but "".charCodeAt(0n) throws a TypeError during conversion. Negative indexes do not count from the end.

Do not confuse a code unit with a Unicode code point. In the example, A occupies one code unit, while the emoji occupies two: 55357 and 56832. charCodeAt reads each separately. Use codePointAt to read the code point starting at a given index; the index is still measured in UTF-16 code units.

This is an ECMAScript standard-library method, not a browser Web API. It can operate on other values after string conversion through String.prototype.charCodeAt.call(value, index), but null and undefined as value cause a TypeError.

Examples

const text = "A\uD83D\uDE00";
console.log(text.charCodeAt(0));
console.log(text.charCodeAt(1));
console.log(text.charCodeAt(2));
console.log(text.charCodeAt(3));
console.log(text.charCodeAt());

Result

65
55357
56832
NaN
65

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources