Javascript : charCodeAt()

For JavaScript strings, there is also the charCodeAt() function. It provides back the character’s Unicode value at a given string index. The charCodeAt() method takes an index as an input.

Functionstring.charCodeAt(index)
Explanationstring: The string from which to retrieve the Unicode value.
index: The zero-based index of the character whose Unicode value is to be retrieved.

Examples

let str = "Hello, World!";
console.log(str.charCodeAt(0)); // Output: 72
console.log(str.charCodeAt(7)); // Output: 87

For the character “H” in the string “Hello, World!”, the Unicode value at index 0 is 72, as returned by str.charCodeAt(0) in the first example. Similarly, for the character “W” in the same string, in the second example, str.charCodeAt(7) returns the Unicode value of the character at index 7, which is 87.