JavaScript String charCodeAt() Method
Example
Return the Unicode of the first character in a string (the Unicode value for "H"):
var str = "HELLO WORLD";
var n = str.charCodeAt(0);
More "Try it Yourself" examples below.
Definition and Usage
The charCodeAt() method returns the Unicode of the character at the specified index in a string.
The index of the first character is 0, the second character 1, and so on.
Tip: You can use the charCodeAt() method together with the length property to return the Unicode of the last character in a string. The index of the last character is -1, the second last character is -2, and so on (See Example below).
Tip: For more information about Unicode Character Sets, visit our HTML Character Sets reference.
Browser Support
Method | |||||
---|---|---|---|---|---|
charCodeAt() | Yes | Yes | Yes | Yes | Yes |
Syntax
string.charCodeAt(index)
Parameter Values
Parameter | Description |
---|---|
index | Optional. A number representing the index of the character you want to return. If not specified, default value is 0 |
Technical Details
Return Value: | A Number, representing the unicode of the character at the specified index. Note: This method returns "NaN" if there is no character at the specified index, or if the index is less than "0". |
---|---|
JavaScript Version: | ECMAScript 1 |
More Examples
Example
Return the Unicode of the last character in a string (the Unicode value for "D"):
var str = "HELLO WORLD";
var n = str.charCodeAt(str.length-1);
Try it Yourself »