charCodeAt method

The charCodeAt method returns the character code (numerical value) at a specific position in a string. Character numbering starts from 0. If the specified number is greater than the last character of the string, then the method returns NaN.

Syntax

javascriptstring.charCodeAt(character number in string);

Example

Let's output the code of the letter 'A' (character with the position 0):

javascriptlet str  = 'ABC';
let code = str.charCodeAt(0);

console.log(code);

The code execution result:

javascript65

Example

Let's look for a character whose number is not in a string:

javascriptlet str  = 'ABC';
let code = str.charCodeAt(4);

console.log(code);

The code execution result:

javascriptNaN

See also

  • the String.fromCharCode method
    that performs the reverse operation
  • the charAt method
    that returns the character at the specified position in a string