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)
  • string: The original string from which to retrieve the character.
  • index: The index position of the character to retrieve. It must be an integer between 0 and string.length - 1.

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.

Javascript




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


Output

l

Contact Us