menu

JavaScript/Typescript - 16 Topics

DEEP DIVE INTO

JavaScript/Typescript

Topic:charCodeAt() method

menu

The charCodeAt() method in JavaScript is used to retrieve the Unicode value (UTF-16 code unit) of a character at a specified index in a string. This method allows you to obtain the numeric representation of a character, which is especially useful when working with text processing, encoding, or comparing characters.

Basic syntax:

javascriptstring.charCodeAt(index)
  • string: The string from which you want to extract the character code.

  • index: The index of the character for which you want to retrieve the Unicode value.

Here's a deep dive into the charCodeAt() method:

Example:

javascriptconst text = "Hello";
const charCode = text.charCodeAt(1);
console.log(charCode); // 101

In this example, the character at index 1 in the string "Hello" is "e," and its Unicode value is 101, which is retrieved using the charCodeAt() method.

Special Characters and Unicode Values:

The charCodeAt() method works for all characters, including special characters and non-Latin characters. It returns the Unicode value for the character at the specified index.

javascriptconst specialChar = "€"; // Euro symbol
const charCode = specialChar.charCodeAt(0);
console.log(charCode); // 8364

Handling Non-String Values:

The charCodeAt() method is designed to work with string values. If you pass a non-string value as the index, it will be coerced into a string and then treated as an index:

javascriptconst text = "Hello";
const index = 2;
const charCode = text.charCodeAt(index.toString());
console.log(charCode); // 108

Handling Nonexistent Indices:

If you provide an out-of-bounds index, the charCodeAt() method will return NaN (Not-a-Number):

javascriptconst text = "Hello";
const charCode = text.charCodeAt(10);
console.log(charCode); // NaN

Unicode Beyond Basic Multilingual Plane (BMP):

JavaScript uses UTF-16 encoding, which means that characters within the Basic Multilingual Plane (BMP) can be represented by a single 16-bit code unit. Characters outside the BMP (e.g., emoji, certain Asian characters) are represented by surrogate pairs, requiring two code units to represent a single character. In such cases, charCodeAt() returns the code unit of the high surrogate.

Converting a Character Code to a Character:

If you have a character code and want to convert it back to the corresponding character, you can use the String.fromCharCode() method:

javascriptconst charCode = 101; // Unicode value for "e"
const character = String.fromCharCode(charCode);
console.log(character); // "e"

The charCodeAt() method is a powerful tool when working with text and character encoding, allowing you to access the numeric representation of characters within a string. It is often used when dealing with text processing, internationalization, or comparing characters in JavaScript applications.

1280 x 720 px