Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript charCodeAt() Method

JavaScript charCodeAt() Method

String Object Reference JavaScript String object

Example

Return the Unicode encoding of the first character in the string (the Unicode value of H):

var str = "HELLO WORLD";

var n = str.charCodeAt(0);

n Output result:

Try it »


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 first character in the string is at position 0, the second at position 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 value

Parameters Description
index Required. A number indicating 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

Return the Unicode encoding of the last character in the string:

var str = "HELLO WORLD";

var n = str.charCodeAt(str.length-1);

n Output result:

Try it »


String Object Reference JavaScript String object