The 'charAt()' method in JavaScript allows us to access a specific character in a string based on its index position. Here's a breakdown:
Syntax: string.charAt(index)
- string: The string from which we want to extract the character.
- index: The position of the character we want to retrieve. Indexing starts from 0 for the first character.
Example:
let str = "Hello, World!";
let charAtIndex7 = str.charAt(7);
console.log(charAtIndex7); // Output: W
Explanation:
- We have a string str containing the text "Hello, World!".
- We use charAt(7) to retrieve the character at index 7, which corresponds to 'W' in the string.
- The method returns the character 'W', which is then logged to the console.
Online Code Compiler: https://app.singhteekam.in/codecompiler/