JavaScript String codePointAt()

The codePointAt() method returns an integer that denotes the Unicode point value of a character in the string.

Example

let message = "Happy Birthday";

// unicode point of character at index 1 let codePoint1 = message.codePointAt(1);
console.log("Unicode Code Point of 'a' is " + codePoint1); // Output // Unicode Code Point of 'a' is 97

codePointAt() Syntax

The syntax of the codePointAt() method is:

str.codePointAt(pos)

Here, str is a string.


codePointAt() Parameters

The codePointAt() method takes a single parameter:

  • pos - index value of an element in str

codePointAt() Return Value

The codePointAt() method returns:

  • a number representing the unicode point value of the character at the given pos
  • undefined if no element is found at pos

Example 1: Using codePointAt() method

let fruit = "Apple";

// unicode code point of character A let codePoint = fruit.codePointAt(0);
console.log("Unicode Code Point of 'A' is " + codePoint);

Output

Unicode Code Point of 'A' is 65

In the above example, we are using the codePointAt() method to find the unicode code point of the character 'A'.

'A' is the first element of the string and since indexing of the string starts from 0, we have passed parameter 0 to the method. The fruit.codePointAt(0) code returns the unicode code point of 'A' which is 65.

Note: Unicode code point is a number value for each character which is defined by an international standard. For example, the unicode value for letter A is 65, B is 66, C is 67, and so on.


Example 2: codePointAt() with Default Parameter

let message = "Happy Birthday";

// without passing parameter in codePointAt() let codePoint = message.codePointAt();
console.log(codePoint);
// passing 0 as parameter let codePoint0 = message.codePointAt(0);
console.log(codePoint0);

Output

72
72

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

So the method returns a Unicode code point of character at index 0 i.e. 72.


Example 3: codePointAt() with Index Value Out of Range

let message = "Happy Birthday";

// get unicode point of character at index 53 let codePoint53 = message.codePointAt(53);
console.log(codePoint53);

Output

undefined

In the above example, we have used the codePointAt() method to access the unicode point of character at index 53.

However, the string "Happy Birthday" doesn't have any character at index 53. Hence, message.codePointAt(53) returns undefined.


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