DEV Community

loading...

String charAt() method

Henry Arbolaez
I'm a coffee lover, thinker, and a software engineer. I'm passionate about the idea of bringing people together to collaborate, to build a great product.
・1 min read

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

Discussion (0)