The charCodeAt() method in JavaScript returns the Unicode value of the character at a specified index within a string.
Syntax: string.charCodeAt(index)
- string: The string from which we want to get the Unicode value.
- index: The position of the character whose Unicode value we want to retrieve. Indexing starts from 0 for the first character.
Example:
let str = "Hello";
let unicodeValue = str.charCodeAt(1);
console.log(unicodeValue); // Output: 101
Explanation:
- We have a string str containing the text "Hello".
- We use charCodeAt(1) to retrieve the Unicode value of the character at index 1, which corresponds to 'e' in the string.
- The method returns the Unicode value of 'e', which is 101, and it is then logged to the console.
Online Code compiler: https://app.singhteekam.in/codecompiler/