Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

String.prototype.charAt()

BaselineWidely available

ThecharAt() method ofString values returns a new string consisting of the single UTF-16 code unit at the given index.

charAt() always indexes the string as a sequence ofUTF-16 code units, so it may return lone surrogates. To get the full Unicode code point at the given index, useString.prototype.codePointAt() andString.fromCodePoint().

Try it

const sentence = "The quick brown fox jumps over the lazy dog.";const index = 4;console.log(`The character at index ${index} is ${sentence.charAt(index)}`);// Expected output: "The character at index 4 is q"

Syntax

js
charAt(index)

Parameters

index

Zero-based index of the character to be returned.Converted to an integerundefined is converted to 0.

Return value

A string representing the character (exactly one UTF-16 code unit) at the specifiedindex. Ifindex is out of the range of0str.length - 1,charAt() returns an empty string.

Description

Characters in a string are indexed from left to right. The index of the first character is0, and the index of the last character in a string calledstr isstr.length - 1.

Unicode code points range from0 to1114111 (0x10FFFF).charAt() always returns a character whose value is less than65536, because the higher code points are represented bya pair of 16-bit surrogate pseudo-characters. Therefore, in order to get a full character with value greater than65535, it is necessary to retrieve not onlycharAt(i), but alsocharAt(i + 1) (as if manipulating a string with two characters), or to usecodePointAt(i) andString.fromCodePoint() instead. For information on Unicode, seeUTF-16 characters, Unicode code points, and grapheme clusters.

charAt() is very similar to usingbracket notation to access a character at the specified index. The main differences are:

  • charAt() attempts to convertindex to an integer, while bracket notation does not, and directly usesindex as a property name.
  • charAt() returns an empty string ifindex is out of range, while bracket notation returnsundefined.

Examples

Using charAt()

The following example displays characters at different locations in the string"Brave new world":

js
const anyString = "Brave new world";console.log(`The character at index 0   is '${anyString.charAt()}'`);// No index was provided, used 0 as defaultconsole.log(`The character at index 0   is '${anyString.charAt(0)}'`);console.log(`The character at index 1   is '${anyString.charAt(1)}'`);console.log(`The character at index 2   is '${anyString.charAt(2)}'`);console.log(`The character at index 3   is '${anyString.charAt(3)}'`);console.log(`The character at index 4   is '${anyString.charAt(4)}'`);console.log(`The character at index 999 is '${anyString.charAt(999)}'`);

These lines display the following:

The character at index 0   is 'B'The character at index 0   is 'B'The character at index 1   is 'r'The character at index 2   is 'a'The character at index 3   is 'v'The character at index 4   is 'e'The character at index 999 is ''

charAt() may return lone surrogates, which are not valid Unicode characters.

js
const str = "𠮷𠮾";console.log(str.charAt(0)); // "\ud842", which is not a valid Unicode characterconsole.log(str.charAt(1)); // "\udfb7", which is not a valid Unicode character

To get the full Unicode code point at the given index, use an indexing method that splits by Unicode code points, such asString.prototype.codePointAt() andspreading strings into an array of Unicode code points.

js
const str = "𠮷𠮾";console.log(String.fromCodePoint(str.codePointAt(0))); // "𠮷"console.log([...str][0]); // "𠮷"

Note:Avoid re-implementing the solutions above usingcharAt(). The detection of lone surrogates and their pairing is complex, and built-in APIs may be more performant as they directly use the internal representation of the string. Install a polyfill for the APIs mentioned above if necessary.

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-string.prototype.charat

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp