Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

String.prototype.codePointAt()

BaselineWidely available

ThecodePointAt() method ofString values returns a non-negative integer that is the Unicode code point value of the character starting at the given index. Note that the index is still based on UTF-16 code units, not Unicode code points.

Try it

const icons = "☃★♲";console.log(icons.codePointAt(1));// Expected output: "9733"

Syntax

js
codePointAt(index)

Parameters

index

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

Return value

A non-negative integer representing the code point value of the character at the givenindex.

  • Ifindex is out of the range of0str.length - 1,codePointAt() returnsundefined.
  • If the element atindex is a UTF-16 leading surrogate, returns the code point of the surrogatepair.
  • If the element atindex is a UTF-16 trailing surrogate, returnsonly the trailing surrogate code unit.

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). In UTF-16, each string index is a code unit with value065535. Higher code points are represented bya pair of 16-bit surrogate pseudo-characters. Therefore,codePointAt() returns a code point that may span two string indices. For information on Unicode, seeUTF-16 characters, Unicode code points, and grapheme clusters.

Examples

Using codePointAt()

js
"ABC".codePointAt(0); // 65"ABC".codePointAt(0).toString(16); // 41"😍".codePointAt(0); // 128525"\ud83d\ude0d".codePointAt(0); // 128525"\ud83d\ude0d".codePointAt(0).toString(16); // 1f60d"😍".codePointAt(1); // 56845"\ud83d\ude0d".codePointAt(1); // 56845"\ud83d\ude0d".codePointAt(1).toString(16); // de0d"ABC".codePointAt(42); // undefined

Looping with codePointAt()

Because using string indices for looping causes the same code point to be visited twice (once for the leading surrogate, once for the trailing surrogate), and the second timecodePointAt() returnsonly the trailing surrogate, it's better to avoid looping by index.

js
const str = "\ud83d\udc0e\ud83d\udc71\u2764";for (let i = 0; i < str.length; i++) {  console.log(str.codePointAt(i).toString(16));}// '1f40e', 'dc0e', '1f471', 'dc71', '2764'

Instead, use afor...of statement orspread the string, both of which invoke the string's[Symbol.iterator](), which iterates by code points. Then, usecodePointAt(0) to get the code point of each element.

js
for (const codePoint of str) {  console.log(codePoint.codePointAt(0).toString(16));}// '1f40e', '1f471', '2764'[...str].map((cp) => cp.codePointAt(0).toString(16));// ['1f40e', '1f471', '2764']

Specifications

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

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp