codePointAt() Method – How to Convert String to Unicode Code Point
Whenever you use codePointAt() on a string, the method does the following:
- It parses (analyzes) the character at the specified index of the calling string.
- It returns the Unicode code point representation of the parsed character.
Syntax of the codePointAt() Method
Section titled “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:
0isindex’s default value. Therefore,codePointAt()will return the zeroth index character’s code point if you do not provide an argument.- The
codePointAt()method returnsundefinedif it finds no character at the specifiedindex.
Examples
Section titled “Examples”Below are examples of the codePointAt() method.
Convert a string’s second indexed character to a Unicode code point
Section titled “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: 117The 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
Section titled “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: 49The 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
Section titled “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: 128159The 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
Section titled “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: undefinedThe 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
Section titled “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: 89The 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
Section titled “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: 46The snippet above returned the code point of aboutYou’s last indexed character (full stop).