Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

JavaScript Dictionary

  1. Home
  2. JavaScript Dictionary
  3. String .charAt() / at()

String .charAt() / at()

Since: charAt() ES1(ECMAScript 1997)
at() ES2022(ECMAScript 2022)

Returns the character at the specified position in a string. To get the character code instead, use string.charCodeAt().

Syntax

// Returns the character at the specified position.
string.charAt(position)

// Returns the character at the specified position. A negative value counts from the end.
string.at(position)

// Returns the UTF-16 code of the character at the specified position.
string.charCodeAt(position)

Method List

MethodDescription
charAt(position)Returns the character at the specified position. Returns an empty string "" if the position is out of range.
at(position)Returns the character at the specified position. A negative value counts from the end of the string. Returns undefined if out of range.
charCodeAt(position)Returns the UTF-16 code (a number) of the character at the specified position. Returns NaN if out of range.

Sample Code

sample_charAt.js
var str = "Hello";

// Get a character using charAt.
console.log(str.charAt(0));  // Outputs "H".
console.log(str.charAt(4));  // Outputs "o".
console.log(str.charAt(10)); // Out of range, so outputs an empty string "".

// Get a character using at. A negative value counts from the end.
console.log(str.at(0));  // Outputs "H".
console.log(str.at(-1)); // Outputs "o" (last character).
console.log(str.at(-2)); // Outputs "l" (second from the end).

// Get the character code using charCodeAt.
console.log(str.charCodeAt(0)); // Outputs the UTF-16 code of "H", which is 72.
console.log(str.charCodeAt(1)); // Outputs the UTF-16 code of "e", which is 101.

// You can also access characters using bracket notation.
console.log(str[0]);  // Outputs "H".
console.log(str[-1]); // Bracket notation does not support negative values, so outputs "undefined".
H
o

H
o
l
72
101
H
undefined

Overview

string.charAt() is a basic method that returns a single character at the specified position. The similar method string.at() also accepts negative values, making it convenient when you need to access characters from the end of a string.

You can also access characters using bracket notation (str[0]), but it does not support negative values. To get the last character, you would need to write str[str.length - 1], whereas string.at(-1) lets you do the same thing more concisely.

string.charCodeAt() returns the UTF-16 code of a character as a number. It is useful when comparing or converting characters, but for simply retrieving a character, string.charAt() or string.at() is the more common choice.

Browser Compatibility

Chrome Chrome
49+
Firefox Firefox
57+
Safari Safari
18+
Edge Edge
80+
11 or earlier ×
IE IE
11+
2 or earlier ×
Opera Opera
48+
2 or earlier ×
iOS Safari iOS Safari
18+
Android Browser Android Browser
37+
4 or earlier ×
Chrome Android Chrome Android
36+
17 or earlier ×
Firefox Android Firefox Android
79+
3 or earlier ×

If you find any errors or copyright issues, please .