Last updated: Mar 4, 2024
Reading timeยท3 min
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.
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"
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.
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.
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.
const str = 'bobbyhadz.com'; // โ Counting backwards console.log(str.charAt(str.length - 2)); // ๐๏ธ "o" console.log(str.charAt(str.length - 1)); // ๐๏ธ "m"
An alternative approach is to use bracket notation.
When an index that doesn't exist is specified, undefined
is returned.
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"
[]
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.
console.log(''[5]); // ๐๏ธ undefined
You can also use bracket notation to count backward, simply subtract the number of characters from the string's length.
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.
You can also use the String.at()
method.
For example, String.at(1)
returns the second character in the string.
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
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.
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.
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.
You can learn more about the related topics by checking out the following tutorials: