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
string.charCodeAt(character number in string);
Example
Let's output the code of the letter
'A' (character with the position
0):
let str = 'ABC';
let code = str.charCodeAt(0);
console.log(code);
The code execution result:
65
Example
Let's look for a character whose number is not in a string:
let str = 'ABC';
let code = str.charCodeAt(4);
console.log(code);
The code execution result:
NaN
See also
-
the
String.fromCharCodemethod
that performs the reverse operation -
the
charAtmethod
that returns the character at the specified position in a string