Using String charCodeAt() method to coding in Javascript/JS

This JavaScript tutorial explains how to use the string approach referred to as charCodeAt() with syntax and examples.

Description

In JavaScript, charCodeAt() is a string method that is used to retrieve a Unicode value for a personality at a precise role in a string. Because the charCodeAt() method is a method of the String object, it need to be invoked via a specific 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 personality in string that you desire to retrieve the Unicode price for. The first role in the string is 0 If this parameter is not provided, the charCodeAt() technique will use zero as the default.

Returns

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

The role ought to be between zero and string.length-1. If the function is out of bounds, the charCodeAt() technique will return a unique not-a-number price printed as NaN.

Note

Use the codePointAt() approach if the Unicode value of the persona is no longer representable in a single UTF-16 code unit. The charCodeAt() method does now not change the cost of the unique string.

Example

Let’s take a seem to be 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 known as totn_string that is assigned the string value of ‘TechOnTheNet’. We have then invoked the charCodeAt() approach of the totn_string variable to return the Unicode price for a persona at a specific position.

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

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

84
101
99
104

As you can see, the charCodeAt() technique back a Unicode cost in all four cases. The first call to the charCodeAt() method lower back 84 which is the Unicode fee for the character “T” at function zero The 2nd call lower back 101 which is the Unicode value for the personality ‘e’ at role 1. The 1/3 call returned ninety nine which is the Unicode fee for the persona “c” at position 2 The fourth name again 104 which is the Unicode value for the character “h” at function 3

No Parameter is Provided

Next, let’s see what happens if you don’t furnish a role parameter to the charCodeAt() method.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.charCodeAt());

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

84

When no role parameter is provided, the charCodeAt() method will use 0 as the fee of the position parameter. In this example, the charCodeAt() technique back the Unicode cost of 84 (which is the Unicode cost for ‘T’) when no parameter was once surpassed to the method.

Parameter is Out of Bounds

Finally, let’s see what occurs if the charCodeAt() approach is passed a function 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 net browser console log:

NaN

Because the first role in the string is 0, the position parameter have to be a fee between zero and string.length-1. If the role parameter is out of bounds and does not fall in this range, the charCodeAt() technique will return a distinct not-a-number fee printed as NaN.

Since 999 is a function that is out of bounds for the string ‘TechOnTheNet’, the charCodeAt() technique returned NaN in the above example.