String.prototype.charCodeAt
Returns the numeric code of a character.
Available since: ES1
Syntax
str.charCodeAt(index)Parameters
| Parameter | Type | Description |
|---|---|---|
| index | number | The position of the character, counting from zero. |
Return value
numberThe UTF-16 code of the character (0 to 65535), or NaN if there is no such position.
Examples
Letter codes:
console.log('A'.charCodeAt(0));
console.log('abc'.charCodeAt(1));Output:
65
98Comparing codes shows why capital letters sort before lowercase ones:
console.log('Z'.charCodeAt(0), 'a'.charCodeAt(0));Output:
90 97Notes
The reverse conversion is String.fromCharCode(). For emoji use codePointAt(): it returns the full character code.
See also
- String.fromCharCode — Creates a string from character codes.
- String.prototype.charAt — Returns the character at a position.
In the tutorial
- String methods — Useful string methods in JavaScript: finding a position, reading characters, padStart, repeat, split and join, method chains.