Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

String.prototype.charAt

Returns the character at a position.

Available since: ES1

Syntax

str.charAt(index)

Parameters

ParameterTypeDescription
indexnumberThe 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:

O

A position outside the string gives an empty string:

console.log('Oleg'.charAt(10) === '');
console.log('Oleg'[10]);

Output:

true
undefined

Notes

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

In the tutorial

  • Strings — Strings in JavaScript — quotes, joining, template literals, length, accessing characters and the first methods.

Thanks for reading!

Donate to the site — we work hard for you.