.charCodeAt()

Published Jun 13, 2024
Contribute to Docs

In JavaScript, the .charCodeAt() method, used with strings, returns a positive integer representing the UTF-16 code unit of the character at a specified index in the string. This integer represents the Unicode value of the character.

Syntax

string.charCodeAt(index)
  • string: The string to be checked.
  • index: The position of the character in the string whose UTF-16 code unit is to be retrieved.

Example

In the following example, a string variable called sentence stores a sentence. Then, the .charCodeAt() method is used to get the Unicode value of a character in the string:

let sentence = 'A sample sentence for demoing how the method works';
console.log(sentence.charCodeAt(0));

This results in the following output:

65

Codebyte Example

Here is a codebyte example that demonstrates the usage of the .charCodeAt() method:

Code
Output
Loading...

All contributors

Looking to contribute?

Learn JavaScript on Codecademy