Home > Article > Web Front-end > Introductory example of JavaScript charCodeAt method (used to obtain the Unicode encoding of the character at the specified position)_Basic knowledge
JavaScript charCodeAt method
The charCodeAt method is used to obtain the Unicode encoding of the character at the specified position from the string. Its syntax is as follows:
Parameter description:
参数 | 说明 |
---|---|
str_object | 要操作的字符串(对象) |
x | 必需。表示位置的数字 |
Tip: String counting starts from 0.
charCodeAt method instance
Run this example, output:
If the parameter x is not between 0 and the maximum length of the string, this method will return NaN.
The charCodeAt method is very similar to the charAt method, except that the former returns the encoding of the character at the specified position, while the latter returns a character substring.