# String - charAt()

Welcome back to our Web Basics, a series on essential programming topics every web developer should absolutely know 🍏

Let’s step up our game a bit today with charAt() - This method returns the character at the specified index of a string.

charAt() — the default is 0. That means it returns the first letter. Remember, array in JS are 0-indexed. So if you leave it empty or try to pass it anything else such as a string, null, undefined, false, the default will take over and you will get the first letter.

const name = 'samantha';

name.charAt(0); // 's'

# Examples

const text = 'Web Basics';

text.charAt(); // Remember, default is 0
// 'W'

text.charAt(text.length - 1); // Get the last letter
// 's'

text.charAt(1000); // Out of rand index
// ''

So charAt() accepts numbers. What happens if you try to pass in something else 🤔

const text = 'Web Basics';

text.charAt('B'); // 'W'
text.charAt(null); // 'W'

If you try to pass in anything else, it would just default to 0 and return the first letter.

# Resources

Code Snippet of String - charAt()

More Courses