[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/String/charAt [Back]  [Original]

String.prototype.charAt() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

String.prototype.charAt()

Baseline

20157

charAt() String UTF-16

charAt() UTF-16 Unicode String.prototype.codePointAt() String.fromCodePoint()

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)}`);
// : "The character at index 4 is q"

js
charAt(index)

index

undefined 0

index 1 UTF-16 index charAt()

0 str str.length - 1

Unicode 0 1114111 (0x10FFFF) charAt() 65536 16 65535 charAt(i) charAt(i + 1) 2 codePointAt(i) String.fromCodePoint() Unicode UTF-16 Unicode

charAt()

  • charAt() index index
  • charAt() index undefined

`"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 default

console.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)}'`);

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() Unicode

js
const str = "";
console.log(str.charAt(0)); // "\ud842" Unicode 
console.log(str.charAt(1)); // "\udfb7" Unicode 

Unicode String.prototype.codePointAt() Unicode Unicode

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

: charAt() API API

ECMAScript 2027 LanguageSpecification
# sec-string.prototype.charat


Web Proxy Viewer  |  New URL  |  Original Page