English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript String charAt() method

 JavaScript String Object

charAt()The method returns the character at the specified index in the string.

The index of the first character is 0, the index of the second character is1, and so on.

The index of the last character in the string isstring.length-1.

If the index you provide exceeds this range, JavaScript returns an empty string.

If no index is provided for charAt(), the default value is 0.

Syntax:

string.charAt(index)
var str = 'oldtoolbag.com';
str.charAt(1);// return h
Test and See ‹/›

Browser Compatibility

All browsers fully support the charAt() method:

Method
charAt()IsIsIsIsIs

Parameter Value

ParameterDescription
indexAn integer representing the index of the character to be returned

Technical Details

Return Value:A string that represents the character at the specified index; if the index is not found, an empty string is returned
JavaScript Version:ECMAScript 1

More Examples

Return the last character of the string:

var str = 'oldtoolbag.com';
str.charAt(str.length-1);
Test and See ‹/›

Traverse the string and print each character:

var str = 'oldtoolbag.com';
for(let i=0; i<str.length; i++) {
str.charAt(i);
}
Test and See ‹/›

 JavaScript String Object