charAt method
The charAt method returns a
character at the specified position
in a string. The position is specified
by the method parameter (numbering
starts from zero). If the specified
position is greater than the position
of the last character, an empty string
will be returned.
Syntax
string.charAt(character position);
Example
Let's output a character at the zero position:
let str = 'abcde';
let char = str.charAt(0);
console.log(char);
The code execution result:
'a'
Example
An empty string will now be output because the entered position is greater than the position of the last character:
let str = 'abcde';
let char = str.charAt(30);
console.log(char);
The code execution result:
''
See also
-
the
startsWithmethod
that checks the start of a string -
the
endsWithmethod
that checks the end of a string -
the
indexOfmethod
that searches for the first occurrence of a substring -
the
includesmethod
that searches for a string -
the
atmethod
that searches for a string character