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.
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.
Parameter | Optional/Required | Description |
---|---|---|
index | required | The position of the element in the string to be returned. |
String.codePointAt() returns value of type Number
.
In JavaScript, we can use the codePointAt()
method to get the code point value at a specific index in the string.
For example,
str
with the value 'Hello'
.codePointAt()
method with the index 1
to retrieve the code point value at that index.1
, which is 101
, is stored in the variable codePointAtIndex1
.codePointAtIndex1
to the console using the console.log()
method.const str = 'Hello';
const codePointAtIndex1 = str.codePointAt(1);
console.log(codePointAtIndex1);
101
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,
str
with the value 'Hello'
.codePointAt()
method with the index str.length - 1
to retrieve the code point value of the last character.111
, is stored in the variable codePointOfLastChar
.codePointOfLastChar
to the console using the console.log()
method.const str = 'Hello';
const codePointOfLastChar = str.codePointAt(str.length - 1);
console.log(codePointOfLastChar);
111
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,
str
with the value 'Hello'
.codePointAt()
method with the index 0
to retrieve the code point value of the first character.72
, is stored in the variable codePointOfFirstChar
.codePointOfFirstChar
to the console using the console.log()
method.const str = 'Hello';
const codePointOfFirstChar = str.codePointAt(0);
console.log(codePointOfFirstChar);
72
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.