Related Tags

javascript
communitycreator

# What is codePointAt() in JavaScript?

Dario Vincenzo Tarantini

In JavaScript, the codePointAt() method is used to get a positive integer, which is the UTF-$16$ 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 $1$, the value of the position parameter is $10$, 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 $4$ and $5$ are printed.

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

RELATED TAGS

javascript
communitycreator

CONTRIBUTOR

Dario Vincenzo Tarantini
RELATED COURSES

View all Courses

Keep Exploring

Learn in-demand tech skills in half the time