Tutorials Tonight
Follow Us

charAt in JavaScript - String Method


In this tutorial, you will learn to get a character from a string in javascript, how to use charAt in JavaScript.

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 of the string or you can loop through the string to get all characters of it.

So how will you get a character from a string?
To get character at some index use charAt() method.

charAt method in JavaScipt

charAt() method JavaScript

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

Syntax

string.charAt(index)

It returns a new string that contains the character of a given string at the specified index.

const quote = "Learning to code is learning to create and innovate";
console.log(quote.charAt(0)); // first character
console.log(quote.charAt(4));
console.log(quote.charAt(5));
console.log(quote.charAt(quote.length - 1));// last character
▶ Try It

Index value in charAt method

The index value passed in the string should lie between 0 and string.length - 1, if the index value is beyond this then it returns an empty string ('').

const quote = "Learning to code is learning to create and innovate.";
console.log(quote.charAt(100));// out of range (return '')
console.log(quote.charAt(-1));// return ''
▶ 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.

const quote = "Learning to code is learning to create and innovate.";
console.log(quote.charAt()); // index not given (default index used = 0)
console.log(quote.charAt("X")); // can't be converted to integer
▶ Try It

Getting the first character of all strings in an array

Here we have an array whose elements are string. 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.

const fruits = ["Apple", "Banana", "Cherry", "Dewberry", "Elderberry"];
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.

const quote = "Learning to code is learning to create and innovate.";
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 in a string

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.

const quote = "Learning to code is learning to create and innovate.";
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.

const quote = "Learning to code is learning to create and innovate.";
console.log(quote[-1]);
console.log(quote[100]);

Points to remember:

  1. To get the character at some index value in a string use charAt() method.
  2. You can also access characters by treating the string as an array of character and accessing the character as an array element.
Follow Us

Copyright © Tutorials Tonight. All Rights Are Reserved.