What is codePointAt() in JavaScript?

In JavaScript, the codePointAt() method is used to get a positive integer, which is the UTF-1616 code point value.

Syntax

The syntax of the codePointAt() method is as follows:

codePointAt(position)

Parameters

The position parameter specifies the position of the element inside the provided string for which we want the code point value.

Return value

If it finds the element at the specified position, the function returns an integer that represents the code point value.

Note: If the element is a UTF-16 high surrogate (higher than U+FFFF), the function returns the surrogate pair. Otherwise, it returns the lowest surrogate point.

If it doesnโ€™t find the element, the function will return undefined.

Code

The code below shows how the codePointAt() method works:

console.log("Hello, Educative".codePointAt(10));
// since we are working with utf16 multibyte characters are valid too
console.log("๐Ÿ˜€".codePointAt(0));
console.log("๐Ÿ˜€".codePointAt(1));
// this is out of range
console.log("Hello, Educative ๐Ÿ˜€".codePointAt(100));

Explanation

In line 11, the value of the position parameter is 1010, which corresponds to the character c in the string "Hello, Educative". Consequently, the codePointAt() method returns the corresponding code point value. Similarly, the code point value for the strings in lines 44 and 55 are printed.

The codepointAt() method in line 88 returns undefined, as the position parameter is larger than the length of the string.