Open In App

What is charAt() Method in JavaScript ?

In JavaScript, the charAt() method is used to retrieve the character at a specified index position within a string. It returns the character located at the specified index as a new string.

Syntax:

string.charAt(index)

Example: Here, the charAt() method is called on the str string with an index of 2. This retrieves the character at index 2, which is the third character in the string (since indexing starts from 0). The resulting character "l" is returned and logged to the console.




const str = "Hello, World!";
const charAtIndex2 = str.charAt(2);
 
console.log(charAtIndex2); // Output: "l"

Output
l
Article Tags :