Get the Nth Character of a String in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 4, 2024
3 min

banner

# Get the Nth Character of a String in JavaScript

Use the charAt() method to get the nth character in a string, e.g. str.charAt(1) returns the second character in the string.

The only parameter the charAt method takes is the index of the character to be returned.

index.js
const str = 'bobbyhadz.com'; // โœ… Using charAt console.log(str.charAt(0)); // ๐Ÿ‘‰๏ธ "b" console.log(str.charAt(1)); // ๐Ÿ‘‰๏ธ "o" console.log(str.charAt(2)); // ๐Ÿ‘‰๏ธ "b" // โœ… Counting backwards console.log(str.charAt(str.length - 1)); // ๐Ÿ‘‰๏ธ "m"

get nth character of string

The code for this article is available on GitHub

The String.charAt() method takes an integer between 0 and string.length - 1 and returns the corresponding character.

If the index doesn't exist in the string, the method returns an empty string.

JavaScript indexes are zero-based, so the index of the first character in a string is 0, and the index of the last character is string.length - 1.

When passed an index out of bounds, the String.charAt() method returns an empty string.

index.js
console.log(''.charAt(5)); // ๐Ÿ‘‰๏ธ ""

You can count backward, by subtracting the number of characters from the string's length.

For example, string.length - 2 gives us the second to last character in the string.

index.js
const str = 'bobbyhadz.com'; // โœ… Counting backwards console.log(str.charAt(str.length - 2)); // ๐Ÿ‘‰๏ธ "o" console.log(str.charAt(str.length - 1)); // ๐Ÿ‘‰๏ธ "m"

# Get the Nth Character of a String using bracket notation

An alternative approach is to use bracket notation.

When an index that doesn't exist is specified, undefined is returned.

index.js
const str = 'bobbyhadz.com'; // โœ… Using bracket notation console.log(str[0]); // ๐Ÿ‘‰๏ธ "b" console.log(str[1]); // ๐Ÿ‘‰๏ธ "o" console.log(str[2]); // ๐Ÿ‘‰๏ธ "b" // โœ… Count backwards console.log(str[str.length - 1]); // ๐Ÿ‘‰๏ธ "m"

get nth character of string using bracket notation

The code for this article is available on GitHub
You will most commonly see bracket [] notation being used to access a string at a specific index.

The bracket notation approach differs from the String.charAt() method because it returns undefined when provided an index that doesn't exist in the string.

index.js
console.log(''[5]); // ๐Ÿ‘‰๏ธ undefined

You can also use bracket notation to count backward, simply subtract the number of characters from the string's length.

index.js
const str = 'bobbyhadz.com'; console.log(str[str.length - 1]); // ๐Ÿ‘‰๏ธ "m" console.log(str[str.length - 2]); // ๐Ÿ‘‰๏ธ "o" console.log(str[str.length - 3]); // ๐Ÿ‘‰๏ธ "c"

The String.charAt() method is quite convenient when you need to have a guarantee that a string is going to be returned even if the index is out of bounds.

Dealing with possibly undefined values might make your code more complicated than it needs to be.

# Get the Nth Character of a String using String.at()

You can also use the String.at() method.

For example, String.at(1) returns the second character in the string.

index.js
const str = 'bobbyhadz.com'; console.log(str.at(0)); // ๐Ÿ‘‰๏ธ b console.log(str.at(1)); // ๐Ÿ‘‰๏ธ o // โœ… count backward console.log(str.at(-1)); // ๐Ÿ‘‰๏ธ m console.log(str.at(-2)); // ๐Ÿ‘‰๏ธ o console.log(str.at(-3)); // ๐Ÿ‘‰๏ธ c

get nth character of string using string at

The code for this article is available on GitHub

The String.at() method takes an integer and returns a new string containing the character at the specified position.

The String.at() method allows for positive and negative integers.

You can use negative integers to count back from the end of the string.

index.js
const str = 'bobbyhadz.com'; const firstChar = str.at(0); console.log(firstChar); // ๐Ÿ‘‰๏ธ b console.log(str.at(-1)); // ๐Ÿ‘‰๏ธ m console.log(str.at(-2)); // ๐Ÿ‘‰๏ธ o console.log(str.at(-3)); // ๐Ÿ‘‰๏ธ c

The String.at() method is quite convenient when you need to count backward.

The String.charAt() method and bracket notation approach doesn't support negative indexing. The String.at() method returns undefined when the specified index doesn't exist in the string.

index.js
const str = ''; console.log(str.at(0)); // ๐Ÿ‘‰๏ธ undefined

Both String.at() and bracket notation return undefined when accessing an index out of bounds.

The String.charAt() method is the only option that returns an empty string if you access an index that doesn't exist.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev