Description

The JavaScript method charCodeAt() returns an integer value between 0 and 65535, representing the UTF-16 decimal value of the character at a specified index in a string.

The below table summarizes its usage.

Usage Details
Syntax str.charCodeAt(index)
  • str is a string
Parameters index
  • An integer between 0 and str.length - 1.
  • If the index cannot be converted to an integer or is not provided, then the default value 0 is used.
Result Returns an integer representing the UTF-16 code of the character at the specified index.
  • It always returns a value between 0 and 65536
  • If the Unicode point cannot be represented in a single UTF-16 code (means a value greater than 0xFFFF), then it returns the first part.
Category String Methods

Example 1: Using the method with an integer index value

For a valid integer index value, the method returns the UTF-16 decimal value for the character at the specified index.

var str = "Hello World!";

// Finding UTF-16 decimal value of character "W" at index 6
document.write("Character code at index 6 is : " + str.charCodeAt(6));

Output:

Character code at index 6 is : 87

Example 2: Using the method with a non-integer index value

For a non-integer (or floating-point) index value, the method returns the UTF-16 decimal value for the character at the index determined by ignoring the decimal digits.

var str = "Hello World!";

// Finding UTF-16 decimal value of character "W" at index 6.2
document.write("Character code at index 6.2 is : " + str.charCodeAt(6.2));

// Finding UTF-16 decimal value of character "W" at index 6.9
document.write("Character code at index 6.9 is : " + str.charCodeAt(6.9));

// Finding UTF-16 decimal value of character "W" at index 6
document.write("Character code at index 6 is : " + str.charCodeAt(6));

Output:

Character code at index 6.3 is : 87
Character code at index 6.9 is : 87
Character code at index 6 is : 87

Example 3: Using the method without passing index value

When the index value is not passed, the method returns the UTF-16 decimal value of the character from the default index (which is 0) of the string.

var str = "Hello World!";

// Finding UTF-16 decimal value of character "H" at default index (which is 0), without passing index value.
document.write("Character code at default index is : " + str.charCodeAt());

// Finding UTF-16 decimal value of character "H" at index 0
document.write("Character code at index 0 is : " + str.charCodeAt(0));

Output:

Character code at default index is : 72
Character code at index 0 is : 72

Example 4: Using the method with an index value out of range

When the index value is out of range, the method returns NaN.

var str = "Hello World!";

// Finding UTF-16 decimal value of character at index 100
document.write("Character code at index 100 is : " + str.charCodeAt(100));

// Finding UTF-16 decimal value of character at index -10
document.write("Character code at index -10 is : " + str.charCodeAt(-10));

Output:

Character code at index 100 is : NaN
Character code at index -10 is : NaN

Overall

The JavaScript method charCodeAt() returns the UTF-16 decimal value of the character at a specified index in a string.

Related Links