totn JavaScript

JavaScript: String charCodeAt() method

This JavaScript tutorial explains how to use the string method called charCodeAt() with syntax and examples.

Description

In JavaScript, charCodeAt() is a string method that is used to retrieve a Unicode value for a character at a specific position in a string. Because the charCodeAt() method is a method of the String object, it must be invoked through a particular instance of the String class.

Syntax

In JavaScript, the syntax for the charCodeAt() method is:

string.charCodeAt([position]);

Parameters or Arguments

position
Optional. It is the position of the character in string that you wish to retrieve the Unicode value for. The first position in the string is 0. If this parameter is not provided, the charCodeAt() method will use 0 as the default.

Returns

The charCodeAt() method returns a UTF-16 value (a 16-bit integer between 0 and 65535) that is the Unicode value for a character at a specific position in a string.

The position must be between 0 and string.length-1. If the position is out of bounds, the charCodeAt() method will return a special not-a-number value printed as NaN.

Note

  • Use the codePointAt() method if the Unicode value of the character is not representable in a single UTF-16 code unit.
  • The charCodeAt() method does not change the value of the original string.

Example

Let's take a look at an example of how to use the charCodeAt() method in JavaScript.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.charCodeAt(0));
console.log(totn_string.charCodeAt(1));
console.log(totn_string.charCodeAt(2));
console.log(totn_string.charCodeAt(3));

In this example, we have declared a variable called totn_string that is assigned the string value of 'TechOnTheNet'. We have then invoked the charCodeAt() method of the totn_string variable to return the Unicode value for a character at a specific position.

We have written the output of the charCodeAt() method to the web browser console log, for demonstration purposes, to show what the charCodeAt() method returns.

The following will be output to the web browser console log:

84
101
99
104

As you can see, the charCodeAt() method returned a Unicode value in all four cases. The first call to the charCodeAt() method returned 84 which is the Unicode value for the character "T" at position 0. The second call returned 101 which is the Unicode value for the character 'e' at position 1. The third call returned 99 which is the Unicode value for the character "c" at position 2. The fourth call returned 104 which is the Unicode value for the character "h" at position 3.

No Parameter is Provided

Next, let's see what happens if you don't provide a position parameter to the charCodeAt() method.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.charCodeAt());

The following will be output to the web browser console log:

84

When no position parameter is provided, the charCodeAt() method will use 0 as the value of the position parameter. In this example, the charCodeAt() method returned the Unicode value of 84 (which is the Unicode value for 'T') when no parameter was passed to the method.

Parameter is Out of Bounds

Finally, let's see what happens if the charCodeAt() method is passed a position value that is out of bounds.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.charCodeAt(999));

The following will be output to the web browser console log:

NaN

Because the first position in the string is 0, the position parameter must be a value between 0 and string.length-1. If the position parameter is out of bounds and does not fall in this range, the charCodeAt() method will return a special not-a-number value printed as NaN.

Since 999 is a position that is out of bounds for the string 'TechOnTheNet', the charCodeAt() method returned NaN in the above example.