THE WORLD'S LARGEST WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JavaScript String charCodeAt() Method

JavaScript String Reference JavaScript String Reference

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);

The result of n will be:

Try it Yourself »

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 Required. A number representing the index of the character you want to return

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: 1.2

Examples

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);

The result of n will be:

Try it Yourself »

JavaScript String Reference JavaScript String Reference