Skip to main content

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.
note
  • codePointAt() does not alter the original string.
  • A calling string is a string on which you used codePointAt(). So, in "Hello world!".codePointAt(), "Hello world!" is the calling string.
  • codePointAt() is sometimes written as String.prototype.codePointAt() because it is a method of the String object's prototype property.

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.

How to 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).

How to 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).

How to 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).

How to 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.

How to 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.

How to 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).

tip

To convert a Unicode code point to a JavaScript string, use fromCodePoint().

Overview

codePointAt() is a string method that converts a string character to a Unicode code point.

Join CodeSweetly's Substack