String.prototype.charAt()

charAt(index)
Returns: string · Added in ves6 · Updated March 14, 2026 · String Methods
javascript es6 string character

The charAt() method returns the character at the specified index in a string. Characters are indexed from 0 to length - 1. This is the standard way to access individual characters when you need explicit index-based retrieval.

Syntax

string.charAt(index)
  • index: An integer representing the position of the character to retrieve. If omitted, defaults to 0. If negative or greater than or equal to length, returns an empty string.
  • Returns: A string containing the character at the specified index, or an empty string if the index is out of bounds.

TypeScript Signature

interface String {
  charAt(index: number): string;
}

Basic Examples

const text = "Hello";

console.log(text.charAt(0)); // "H"
console.log(text.charAt(1)); // "e"
console.log(text.charAt(4)); // "o"
console.log(text.charAt(5)); // "" (out of bounds → empty string)
console.log(text.charAt(-1)); // "" (negative → empty string)

charAt() vs Bracket Notation

Two common ways to access a character exist: charAt() and bracket notation string[index]. They behave identically for valid indices, but differ critically when the index is out of bounds:

const text = "Hi";

console.log(text.charAt(0));   // "H"
console.log(text.charAt(1));   // "i"
console.log(text.charAt(5));   // "" (empty string)

console.log(text[0]);          // "H"
console.log(text[1]);          // "i"
console.log(text[5]);          // undefined (not empty string!)

The key difference: bracket notation returns undefined for out-of-bounds indices, while charAt() returns an empty string. This makes charAt() safer when you concatenate the result—you won’t accidentally get the string "undefined" in your output.

Unicode and UTF-16 Code Units

JavaScript strings are UTF-16 encoded. This means each “character” position corresponds to a 16-bit code unit, not necessarily a complete Unicode codepoint:

const emoji = "😀";

console.log(emoji.length);     // 2 (two UTF-16 code units)
console.log(emoji.charAt(0));  // "�" (first surrogate: 0xD83D)
console.log(emoji.charAt(1));  // "�" (second surrogate: 0xDE00)

// Use spread operator or for-of for actual Unicode characters
console.log([..."😀"][0]);      // "😀"

For simple ASCII and common characters, charAt() works as expected. For emojis and supplementary characters, use the spread operator or codePointAt() instead.

Iteration Pattern

A common pattern to iterate over each character in a string:

const str = "hello";

for (let i = 0; i < str.length; i++) {
  console.log(`Index ${i}: ${str.charAt(i)}`);
}
// Output:
// Index 0: h
// Index 1: e
// Index 2: l
// Index 3: l
// Index 4: o

Edge Cases

const text = "test";

console.log(text.charAt());     // "t" (defaults to index 0)
console.log(text.charAt(0));    // "t"
console.log(text.charAt(Infinity)); // "" (out of bounds)
console.log(text.charAt(NaN));  // "t" (NaN coerced to 0)

Browser Compatibility

BrowserVersion
Chrome1+
Firefox1+
Safari1+
Edge12+
Node.js0.1.0+

Universal support across all JavaScript environments.

See Also