The charAt() method returns the character at the specified index from a string.
The charAt() method returns the character at the specified index from a string.
The index of the first character is 0, the second character is 1, and so on.
The index of the last character in a string is string.length-1, the second last character is string.length-2, and so on.
string.charAt(index)
Parameter | Require | Description |
---|---|---|
index | Required. | An integer representing the index of the character you want to return |
A String, representing the character at the specified index, or an empty string if the index number is not found
//Return the first character of a string: var str = "HELLO WORLD"; var res = str.charAt(0) console.log(res);/* w w w.ja v a 2 s .co m*/ //Return the last character of a string: str = "HELLO WORLD"; res = str.charAt(str.length-1); console.log(res);