Javascript - String charCodeAt() Method

The charCodeAt() method returns the Unicode of the character at the specified index in a string.

Description

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.

The index of the last character is -1, the second last character is -2, and so on.

Syntax

string.charCodeAt(index)

Parameter Values

Parameter Require Description
index Required. A number representing the index of the character you want to return

Return

A Number, representing 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".

Demo

//Return the Unicode of the first character in a string (the Unicode value for "H"):
var str = "HELLO WORLD";
var n = str.charCodeAt(0);
console.log(n);/*ww w.j  a va 2  s.c o  m*/
//Return the Unicode of the last character in a string (the Unicode value for "D"):
str = "HELLO WORLD";
n = str.charCodeAt(str.length-1);
console.log(n);

Result