X
HOME HTML CSS JAVASCRIPT SQL PHP JQUERY XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

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 »


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

Internet Explorer Firefox Opera Google Chrome Safari

The charCodeAt() method is supported in all major browsers.


Syntax

string.charCodeAt(index)

Parameter Values

Parameter Description
index Required. An integer representing the index of the character you want to return

Return Value

Type Description
Number The unicode of the character at the specified index.

This method returns "NaN" if there is no character at the specified index, or if the index is less than "0".

Technical Details

JavaScript Version: 1.2


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

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates

SHARE THIS PAGE

facebook