English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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.
string.charAt(index)
var str = 'oldtoolbag.com'; str.charAt(1);// return hTest and See ‹/›
All browsers fully support the charAt() method:
Method | |||||
charAt() | Is | Is | Is | Is | Is |
Parameter | Description |
---|---|
index | An integer representing the index of the character to be returned |
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 |
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 ‹/›