charAt in JavaScript - String Method


In this tutorial, you will learn about charAt in JavaScript, its usage, and how to find what character is at a given location in a string.

The characters in the string are indexed from left to right in JavaScript. Index value starts from 0 and end with string.length - 1.

You can use this index value to get any character present in the string or you can loop through the string to get all characters.

So how will you get a character from a string using its index?🤔
💡To get a character from a string use charAt() method.

charAt method in JavaScript

charAt() method JavaScript

The charAt() is a String method that is used to find a character at the specific index of a string.

It returns a new string with a character present at the given index.

Syntax:

str.charAt(index)

Here str is the string and index is the index value of the character you want to get.

Let's see an example of how to use charAt() method.

Example

var quote = "Learning to code";

// Get the character at index 0
console.log("Character at index 0: " + quote.charAt(0)); // L

console.log("Character at index 2: " + quote.charAt(2)); // a
console.log("Last character: " + quote.charAt(quote.length - 1)); // e
Try It

Index value in charAt method

The index value that you pass to charAt() method should be between 0 and string.length - 1, if not it will return an empty string (' ').

Example

var quote = "Learning to code";

// index out of range (return '')
console.log(quote.charAt(100));
console.log(quote.charAt(-1));
Try It

Also, the default value of the index is 0, which means if the index value is not given or given index value can't be converted to the string then it used 0 as the index and returns the first character of the string.

Example

var quote = "Learning to code";

// index not given (default index used = 0)
console.log(quote.charAt());
// index can't be converted to integer
console.log(quote.charAt("X"));
Try It

Getting the first character of all strings in an array

Here we have an array whose elements are strings. Now we want to get the first character of each element of the array.

To get the first character of each element of the array loop through the array and use the charAt() method on each element.

Example

const fruits = ["Apple", "Banana", "Cherry", "Dewberry", "Elderberry"];

// get the first character of each element string
fruits.forEach(fruit => {
  let char = fruit.charAt(0);
  console.log(char);
})
Try It

Displaying character at different index in String

Here is an example that displays characters at different locations in the string.

Example

const quote = "Learning to code";

console.log("character at index 0 is " + quote.charAt(0));
console.log("character at index 1 is " + quote.charAt(1));
console.log("character at index 2 is " + quote.charAt(2));
console.log("character at index 10 is " + quote.charAt(10));
console.log("character at index 20 is " + quote.charAt(20));
console.log("character at index 100 is " + quote.charAt(100));

Alternate way: find character at given index

The string in JavaScript can be considered as an array of characters. So you can get the characters of the array by accessing it by its index value.

To get the first character you can write string[0], for the second element write string[1], and so on.

Example

const quote = "Learning to code";
console.log(quote[0]);
console.log(quote[2]);
console.log(quote[10]);
Try It

If the index value used is not between 0 and string.length - 1 then it returns undefined.

Example

const quote = "Learning to code";
console.log(quote[-1]);
console.log(quote[100]);

Conclusion

In this short guide, we have learned about charAt in JavaScript. To get the character at some index value in a string use charAt() method. You can also access characters by treating the string as an array of characters and accessing the character as an array element.