Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

String.prototype.charAt — UTF-16 code unit at an index

The charAt() method returns a string containing one UTF-16 code unit at a zero-based index, or an empty string outside the valid range. It does not guarantee a complete Unicode character.

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.charAt(index)

String.prototype.charAt reads the UTF-16 code unit at a zero-based index. It returns a primitive string: a string one code unit long, or "" when the index is outside the valid range. It does not modify the original string.

The index argument undergoes ToIntegerOrInfinity conversion: fractions are truncated toward zero, while an omitted argument, undefined, and NaN mean index 0. A negative index after conversion does not count backward from the end; it produces "". Both infinities also produce "".

A code unit is not necessarily a complete Unicode code point or a user-perceived character. The emoji in the example occupies two code units; text.charAt(1) returns only its first surrogate. Do not use this method to extract complete visual characters.

The method first rejects null and undefined as this, throwing TypeError, and then converts the receiver to a string. It can therefore be borrowed, for example with String.prototype.charAt.call(123, 1), which returns "2".

Examples

const text = "A\uD83D\uDE00B";
console.log(text.charAt(0));
console.log(text.charAt());
console.log(text.charAt(3.9));
console.log(JSON.stringify(text.charAt(-1)));
console.log(JSON.stringify(text.charAt(4)));
console.log(text.charAt(1) === "\uD83D");

Result

A
A
B
""
""
true

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