String charAt()
In JavaScript, String.charAt()
method is used to get the character from the string at given index.
Syntax
str.charAt(n)
where
str
is a string.n
is an integer (position/index).
charAt()
method returns the character from string str
present at the index n
.
Examples
1. In the following program, we get char at index=3 in the string name
.
let name = 'apple';
let output = name.charAt(3);
console.log(output);
//output: l
2. In the following program, we take the value of n
outside the range of string, that is given index is out of the bounds of the string. Then charAt()
returns empty string value.
let name = 'apple';
let output = name.charAt(15);
console.log(output);
//output: