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
| Parameter | Optional/Required | Description |
|---|---|---|
index | required | The 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,
- We define a string variable
strwith the value'Hello'. - We use the
codePointAt()method with the index1to retrieve the code point value at that index. - The code point value at index
1, which is101, is stored in the variablecodePointAtIndex1. - We log
codePointAtIndex1to the console using theconsole.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,
- We define a string variable
strwith the value'Hello'. - We use the
codePointAt()method with the indexstr.length - 1to retrieve the code point value of the last character. - The code point value of the last character, which is
111, is stored in the variablecodePointOfLastChar. - We log
codePointOfLastCharto the console using theconsole.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,
- We define a string variable
strwith the value'Hello'. - We use the
codePointAt()method with the index0to retrieve the code point value of the first character. - The code point value of the first character, which is
72, is stored in the variablecodePointOfFirstChar. - We log
codePointOfFirstCharto the console using theconsole.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.