Skip to content
Latest: The updated version of the Creating NPM Package (React JavaScript) book is out

codePointAt() Method – How to Convert String to Unicode Code Point

Whenever you use codePointAt() on a string, the method does the following:

  1. It parses (analyzes) the character at the specified index of the calling string.
  2. It returns the Unicode code point representation of the parsed character.

Syntax of the codePointAt() Method

codePointAt() accepts only one argument. Here is the syntax:

callingString.codePointAt(index);

The index argument refers to the position of the character whose code point you wish to get.

Note the following:

  • 0 is index’s default value. Therefore, codePointAt() will return the zeroth index character’s code point if you do not provide an argument.
  • The codePointAt() method returns undefined if it finds no character at the specified index.

Examples

Below are examples of the codePointAt() method.

Convert a string’s second indexed character to a Unicode code point

const aboutYou = "You are my number 1 💟ly friend.";
aboutYou.codePointAt(2);
// The invocation above will return: 117

Try Editing It

The snippet above returned the code point of aboutYou’s second indexed character (lowercase letter u).

Convert a string’s eighteenth indexed character to a Unicode code point

const aboutYou = "You are my number 1 💟ly friend.";
aboutYou.codePointAt(18);
// The invocation above will return: 49

Try Editing It

The snippet above returned the code point of aboutYou’s eighteenth indexed character (digit one).

Convert a string’s twentieth indexed character to a Unicode code point

const aboutYou = "You are my number 1 💟ly friend.";
aboutYou.codePointAt(20);
// The invocation above will return: 128159

Try Editing It

The snippet above returned the code point of aboutYou’s twentieth indexed character (heart decoration emoji).

Convert a string’s hundredth indexed character to a Unicode code point

const aboutYou = "You are my number 1 💟ly friend.";
aboutYou.codePointAt(100);
// The invocation above will return: undefined

Try Editing It

The snippet above returned undefined because there is no character at aboutYou’s hundredth index.

Convert a string’s undefined indexed character to a Unicode code point

const aboutYou = "You are my number 1 💟ly friend.";
aboutYou.codePointAt();
// The invocation above will return: 89

Try Editing It

The snippet above returned 89 because the index parameter defaults to 0 whenever you do not provide an argument.

Convert a string’s last indexed character to a Unicode code point

const aboutYou = "You are my number 1 💟ly friend.";
aboutYou.codePointAt(aboutYou.length - 1);
// The invocation above will return: 46

Try Editing It

The snippet above returned the code point of aboutYou’s last indexed character (full stop).