JavaScript String charCodeAt() method

The charCodeAt() method in JavaScript is used to return the Unicode value of the character at a specified index in a string. This method takes a single argument, which is the index of the character to return the Unicode value for. If the index is out of range, the charCodeAt() method returns NaN.

Here is an example of how the charCodeAt() method works:

const string1 = 'Hello, world!';

const result = string1.charCodeAt(7);

// the result will be: 119

In the code example above, we first declare a string called string1 that contains the text Hello, world!. We then use the charCodeAt() method to get the Unicode value of the character at index 7 in the string. The charCodeAt() method takes a single argument, which is the index of the character to return the Unicode value for. In this case, we pass in the number 7 as the argument.

As a result of calling the charCodeAt() method, the result variable will be set to 119, which is the Unicode value of the character at index 7 in the string1 string. If the index we were searching for was out of range (i.e., if we passed in a number greater than the length of the string), the result variable would be set to NaN.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: