Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

String.prototype.charCodeAt()

BaselineWidely available

ThecharCodeAt() method ofString values returns an integer between0 and65535 representing the UTF-16 code unit at the given index.

charCodeAt() 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().

Try it

const sentence = "The quick brown fox jumps over the lazy dog.";const index = 4;console.log(  `Character code ${sentence.charCodeAt(index)} is equal to ${sentence.charAt(    index,  )}`,);// Expected output: "Character code 113 is equal to q"

Syntax

js
charCodeAt(index)

Parameters

index

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

Return value

An integer between0 and65535 representing the UTF-16 code unit value of the character at the specifiedindex. Ifindex is out of range of0str.length - 1,charCodeAt() returnsNaN.

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).charCodeAt() always returns a value that 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 onlycharCodeAt(i), but alsocharCodeAt(i + 1) (as if manipulating a string with two characters), or to usecodePointAt(i) instead. For information on Unicode, seeUTF-16 characters, Unicode code points, and grapheme clusters.

Examples

Using charCodeAt()

The following example returns65, the Unicode value for A.

js
"ABC".charCodeAt(0); // returns 65

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

js
const str = "𠮷𠮾";console.log(str.charCodeAt(0)); // 55362, or d842, which is not a valid Unicode characterconsole.log(str.charCodeAt(1)); // 57271, or dfb7, which is not a valid Unicode character

To get the full Unicode code point at the given index, useString.prototype.codePointAt().

js
const str = "𠮷𠮾";console.log(str.codePointAt(0)); // 134071

Note:Avoid re-implementingcodePointAt() usingcharCodeAt(). The translation from UTF-16 surrogates to Unicode code points is complex, andcodePointAt() may be more performant as it directly uses the internal representation of the string. Install a polyfill forcodePointAt() if necessary.

Below is a possible algorithm to convert a pair of UTF-16 code units into a Unicode code point, adapted from theUnicode FAQ:

js
// constantsconst LEAD_OFFSET = 0xd800 - (0x10000 >> 10);const SURROGATE_OFFSET = 0x10000 - (0xd800 << 10) - 0xdc00;function utf16ToUnicode(lead, trail) {  return (lead << 10) + trail + SURROGATE_OFFSET;}function unicodeToUTF16(codePoint) {  const lead = LEAD_OFFSET + (codePoint >> 10);  const trail = 0xdc00 + (codePoint & 0x3ff);  return [lead, trail];}const str = "𠮷";console.log(utf16ToUnicode(str.charCodeAt(0), str.charCodeAt(1))); // 134071console.log(str.codePointAt(0)); // 134071

Specifications

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

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp