String.prototype.charAt
Returns the character at a position.
Available since: ES1
Syntax
str.charAt(index)Parameters
| Parameter | Type | Description |
|---|---|---|
| index | number | The position of the character, counting from zero. |
Return value
stringA one-character string, or an empty string if there is no such position.
Examples
The first letter of a name:
console.log('Oleg'.charAt(0));Output:
OA position outside the string gives an empty string:
console.log('Oleg'.charAt(10) === '');
console.log('Oleg'[10]);Output:
true
undefinedNotes
Today people usually write str[0] or str.at(0). The difference: for a missing position charAt() returns an empty string, while square brackets give undefined.
See also
- String.prototype.at — Returns the character at a position; negative indexes are allowed.
- String.prototype.charCodeAt — Returns the numeric code of a character.
In the tutorial
- Strings — Strings in JavaScript — quotes, joining, template literals, length, accessing characters and the first methods.