JavaScript charCodeAt()Method
Example
Returns the Unicode encoding of the first character in the string (the Unicode value of H):
var str = "HELLO WORLD";
var n = str.charCodeAt(0);
var n = str.charCodeAt(0);
nOutput result:
Try it Yourself »
Definition and Usage
The charCodeAt() method returns the Unicode encoding of the character at the specified position. The return value is an integer between 0 and 65535, representing the UTF-16 code unit at the given index.
The position of the first character in a string is 0, the second character is 1, and so on.
Browser Support
The numbers in the table indicate the first browser version that supports this method.
| Method | |||||
|---|---|---|---|---|---|
| charCodeAt() | Yes | Yes | Yes | Yes | Yes |
Syntax
string.charCodeAt(index)
Parameter Values
| Parameter | Description |
|---|---|
| index | Required. A number representing a position in the string, i.e., the index of the character in the string. |
Return Value
| Type | Description |
|---|---|
| Number | Returns the Unicode encoding of the character at the specified position. |
Technical Details
| JavaScript Version: | 1.2 |
|---|
More Examples
Example
Returns the Unicode encoding of the last character in the string:
var str = "HELLO WORLD";
var n = str.charCodeAt(str.length-1);
var n = str.charCodeAt(str.length-1);
nOutput result:
Try it Yourself »
Other Extensions
JavaScript String Object