JavaScript String charCodeAt()

The charCodeAt() method returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index.

Example

// string definition
const greeting = "Good morning!";

// UTF-16 code unit of character at index 5 let result = greeting .charCodeAt(5);
console.log(result); // Output: 109

charCodeAt() Syntax

The syntax of the charCodeAt() method is:

str.charCodeAt(index)

Here, str is a string.


charCodeAt() Parameters

The charCodeAt() method takes a single parameter:

  • index - An integer between 0 and (str.length - 1).

Note: The str.length returns the length of a given string.


charCodeAt() Return Value

  • Returns a number representing the UTF-16 code unit value of the character at the given index.

Notes:

  • The charCodeAt() method always returns a value less than 65,536.
  • If a Unicode point cannot be represented in a single UTF-16 code unit (values greater than 0xFFFF), then it returns the first part of a pair for the code point.

Example 1: Using charCodeAt() Method

const greeting = "Good morning!";

// UTF-16 code unit of character at index 5 let result1 = greeting .charCodeAt(5);
console.log(result1);
// UTF-16 code unit of character at index 5.2 let result2 = greeting .charCodeAt(5.2);
console.log(result2);
// UTF-16 code unit of character at index 5.9 let result3 = greeting.charCodeAt(5.9);
console.log(result3);

Output

109
109
109

In the above example, we are using the charCodeAt() method to access the UTF-16 code unit of the character at index 5.

Since the character present at index 5 is "m", the method returns UTF-16 code unit of "m".

Similarly, for the non-integer index 5.2 and 5.9, the numbers are converted to nearest integer value i.e. 5, so the method again returns UTF-16 code unit of "m" i.e. 109.


Example 2: charCodeAt() Method for Index Out of Range

const greeting = "Good morning!";

// passing index greater than length of string let result3 = greeting.charCodeAt(18);
console.log(result3);
// passing non-negative index value let result4 = greeting.charCodeAt(-9);
console.log(result4);

Output

NaN
NaN

In the above example, we have created a string "Good morning!".

Here, both the code greeting.charCodeAt(18) and greeting.charCodeAt(-9) returns NaN because both indexes 18 and -9 are not present in the given string.


Example 3: charCodeAt() with Default Parameter

const greeting = "Good morning!";

// without passing parameter in charCodeAt() let result1 = greeting.charCodeAt();
console.log(result1);
// passing 0 as parameter let result2 = greeting.charCodeAt(0);
console.log(result2);

Output

71
71

In the above example, since we have not passed any parameter in charCodeAt(), the default value will be 0.

So the method returns UTF-16 code unit of character at index 0 i.e. 71.


Also Read:

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges