Home » JavaScript String charCodeAt() Method

JavaScript String charCodeAt() Method

by Online Tutorials Library

JavaScript String charCodeAt() Method

The JavaScript string charCodeAt() method is used to find out the Unicode value of a character at the specific index in a string.

The index number starts from 0 and goes to n-1, where n is the length of the string. It returns NaN if the given index number is either a negative number or it is greater than or equal to the length of the string.

Syntax

The charCodeAt() method is represented by the following syntax:

Parameter

index – It represent the position of a character.

Return

A Unicode value

JavaScript String charCodeAt() Method Example

Let’s see some simple examples of charCodeAt() method.

Example 1

Here, we will print the Unicode value of a character by passing its specific index.

Test it Now

Output:

97  

Example 2

In this example, we will not pass any index number with the method. In such case, it will return the Unicode value of first character.

Test it Now

Output:

74  

Example 3

Here, we will print the Unicode value of last character in a string.

Test it Now

Output:

116  

Example 4

Here, we will provide the index number greater than the length of the string. In such case, the method returns NaN.

Test it Now

Output:

NaN  

Next TopicJavaScript String

You may also like