totn JavaScript

JavaScript: String codePointAt() method

This JavaScript tutorial explains how to use the string method called codePointAt() with syntax and examples.

Description

In JavaScript, codePointAt() is a string method that is used to retrieve the Unicode code point (that may not be representable in a single UTF-16 code unit) for a character at a specific position in a string. Because the codePointAt() method is a method of the String object, it must be invoked through a particular instance of the String class.

Syntax

In JavaScript, the syntax for the codePointAt() method is:

string.codePointAt([position]);

Parameters or Arguments

position
Optional. It is the position of the character in string that you wish to retrieve the Unicode code points for. The first position in the string is 0. If this parameter is not provided, the codePointAt() method will use 0 as the default.

Returns

The codePointAt() method returns a Unicode code point for a character at a specific position in a string.

If the position is out of bounds and there is no character at the given position, the codePointAt() method will return undefined.

Note

  • You can also use the charCodeAt() method if the Unicode value of the character is representable in a single UTF-16 code unit.
  • The codePointAt() method does not change the value of the original string.

Example

Let's take a look at an example of how to use the codePointAt() method in JavaScript.

For example:

console.log('𠮷'.codePointAt(0));
console.log('𠮷'.codePointAt(1));

In this example, we have invoked the codePointAt() method to return the Unicode code point for a character at a specific position.

We have written the output of the codePointAt() method to the web browser console log, for demonstration purposes, to show what the codePointAt() method returns.

The following will be output to the web browser console log:

134071
57271

As you can see, the codePointAt() method returned a Unicode code point in both cases. The "𠮷" character is not representable in a single UTF-16 code unit, so it has a surrogate pair of code points.

No Parameter is Provided

Next, let's see what happens if you don't provide a parameter to the codePointAt() method.

For example:

console.log('𠮷'.codePointAt(0));

The following will be output to the web browser console log:

134071

When no position parameter is provided, the codePointAt() method will use 0 as the value of the position parameter. In this example, the codePointAt() method returned the Unicode code point of 134071 when no parameter was passed to the method.

Parameter is Out of Bounds

Finally, let's see what happens if the codePointAt() method is passed a position value that is out of bounds.

For example:

console.log('𠮷'.codePointAt(2));

The following will be output to the web browser console log:

undefined

If the position parameter is out of bounds and there is no character at the given position, the codePointAt() method will return undefined.

Since 2 is a position that is out of bounds for the string '𠮷', the codePointAt() method returned undefined in the above example.