Get the Unicode of the first character in a string:
let text = "HELLO WORLD";
let code = text.charCodeAt(0);
Try it Yourself »
Get the Unicode of the second:
let text = "HELLO WORLD";
let code = text.charCodeAt(1);
Try it Yourself »
More examples below.
The charCodeAt()
method returns the Unicode of the character at a specified index (position) in a string.
The index of the first character is 0, the second is 1, ....
The index of the last character is string length - 1 (See Examples below).
See also the charAt()
method.
charCodeAt()
is UTF-16, codePointAt()
is Unicode.
charCodeAt()
returns a number between 0 and 65535.
Both methods return an integer representing the UTF-16 code of a character, but only codePointAt()
can return the full value of a Unicode value greather 0xFFFF (65535).
For more information about Unicode Character Sets, visit our Unicode Reference.
string.charCodeAt(
index)
Parameter | Description |
index | Optional. A number. The index (position) of a character. Default value = 0. |
Type | Description |
A number | The Unicode of the character at the specified index. NaN if the index is invalid. |
Get the Unicode of the last character in a string:
let text = "HELLO WORLD";
let code = text.charCodeAt(text.length-1);
Try it Yourself »
Get the Unicode of the 15th character:
let text = "HELLO WORLD";
let code = text.charCodeAt(15);
Try it Yourself »
charCodeAt()
is an ECMAScript1 (ES1) feature.
ES1 (JavaScript 1997) is fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!