JavaScript String.prototype.charCodeAt() Method
The charCodeAt() method returns the numeric UTF-16 code of the character at a given index.
Description
The charCodeAt() method returns the UTF-16 code unit at the index, a number between 0 and 65535. For characters made of two code units (surrogate pairs), it returns only half of the pair; use codePointAt() to get the full Unicode code point.
Character codes are useful for simple checks and transformations, such as testing whether a character is an ASCII letter (codes 65 to 90 and 97 to 122) or shifting letters in a Caesar cipher. For readability, comparing characters directly, or using a regular expression, is often clearer than working with numbers.
If the index is outside the string, it returns NaN. When you need to know whether a code is one half of a surrogate pair, check whether it's in the range 0xD800 to 0xDFFF; codePointAt() deals with pairs for you.
Syntax
string.charCodeAt(index)Parameters
- indexThe position of the character, from 0. Defaults to 0.
Return Value
A number from 0 to 65535 (the UTF-16 code unit), or NaN if the index is out of range.
Example
Browser Support
The following information will show you the current browser support for the String.prototype.charCodeAt() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





