JavaScript Tutorials

JavaScript String codePointAt()
Syntax & Examples

String.codePointAt() method

The codePointAt() method of the String class in JavaScript returns a nonnegative integer Number that is the code point value of the UTF-16 encoded code point starting at the specified pos.


Syntax of String.codePointAt()

The syntax of String.codePointAt() method is:

codePointAt(index)

This codePointAt() method of String returns a nonnegative integer Number that is the code point value of the UTF-16 encoded code point starting at the specified pos.

Parameters

ParameterOptional/RequiredDescription
indexrequiredThe position of the element in the string to be returned.

Return Type

String.codePointAt() returns value of type Number.



✐ Examples

1 Using codePointAt() method to get the code point value at a specific index

In JavaScript, we can use the codePointAt() method to get the code point value at a specific index in the string.

For example,

  1. We define a string variable str with the value 'Hello'.
  2. We use the codePointAt() method with the index 1 to retrieve the code point value at that index.
  3. The code point value at index 1, which is 101, is stored in the variable codePointAtIndex1.
  4. We log codePointAtIndex1 to the console using the console.log() method.

JavaScript Program

const str = 'Hello';
const codePointAtIndex1 = str.codePointAt(1);
console.log(codePointAtIndex1);

Output

101

2 Using codePointAt() method to get the code point value of the last character

In JavaScript, we can use the codePointAt() method with the index str.length - 1 to get the code point value of the last character in the string.

For example,

  1. We define a string variable str with the value 'Hello'.
  2. We use the codePointAt() method with the index str.length - 1 to retrieve the code point value of the last character.
  3. The code point value of the last character, which is 111, is stored in the variable codePointOfLastChar.
  4. We log codePointOfLastChar to the console using the console.log() method.

JavaScript Program

const str = 'Hello';
const codePointOfLastChar = str.codePointAt(str.length - 1);
console.log(codePointOfLastChar);

Output

111

3 Using codePointAt() method to get the code point value of the first character

In JavaScript, we can use the codePointAt() method with the index 0 to get the code point value of the first character in the string.

For example,

  1. We define a string variable str with the value 'Hello'.
  2. We use the codePointAt() method with the index 0 to retrieve the code point value of the first character.
  3. The code point value of the first character, which is 72, is stored in the variable codePointOfFirstChar.
  4. We log codePointOfFirstChar to the console using the console.log() method.

JavaScript Program

const str = 'Hello';
const codePointOfFirstChar = str.codePointAt(0);
console.log(codePointOfFirstChar);

Output

72

Summary

In this JavaScript tutorial, we learned about codePointAt() method of String: the syntax and few working examples with output and detailed explanation for each example.