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.
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 asString.prototype.codePointAt()
because it is a method of theString
object'sprototype
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
isindex
's default value. Therefore,codePointAt()
will return the zeroth index character's code point if you do not provide an argument.- The
codePointAt()
method returnsundefined
if it finds no character at the specifiedindex
.
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
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
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
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
The snippet above returned undefined
because there is no character at aboutYou
's hundredth index.
Create NPM Package like a pro
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
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
The snippet above returned the code point of aboutYou
's last indexed character (full stop).
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.
Your support matters: Buy me a coffee to support CodeSweetly's mission of simplifying coding concepts.
Tweet this article