DEV Community

Henry Arbolaez
Henry Arbolaez

Posted on

String charAt() method

String charAt() method

Learn about the charAt() string method. Which will return a new string consisting of the single character located base on it index.


Note: If no index is proved to charAt() it will fallback to 0

Using charAt() without index

const city = 'Miami';
console.log(city.charAt());
// charAt() using default index => 0
/**
 * @return 'M'
 */
Enter fullscreen mode Exit fullscreen mode

Using charAt() index

const country = 'USA';
country.charAt(1);
/**
 * @return 'U'
 */
Enter fullscreen mode Exit fullscreen mode

Using charAt() with a outbound index

// note: if the index pass to chartAt(999) is not in the string length
// this will return empty string

const country = 'USA';
country.charAt(999);
/**
 * @return ""
 */
Enter fullscreen mode Exit fullscreen mode

Top comments (0)