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

javascriptstring.charAt(character position);

Example

Let's output a character at the zero position:

javascriptlet str  = 'abcde';
let char = str.charAt(0);

console.log(char);

The code execution result:

javascript'a'

Example

An empty string will now be output because the entered position is greater than the position of the last character:

javascriptlet str  = 'abcde';
let char = str.charAt(30);

console.log(char);

The code execution result:

javascript''

See also

  • the startsWith method
    that checks the start of a string
  • the endsWith method
    that checks the end of a string
  • the indexOf method
    that searches for the first occurrence of a substring
  • the includes method
    that searches for a string
  • the at method
    that searches for a string character