The syntax of the codePointAt()
method is:
str.codePointAt(pos)
Here, str is a string.
codePointAt() Parameters
The codePointAt()
method takes in :
- pos - Position of an element in
str
to return the code point value from.
Return value from codePointAt()
- Returns a number representing the code point value of the character at the given pos.
- Returns
undefined
if no element is found at pos.
Example: Using codePointAt() method
let sentence = "Happy Birthday";
let codePt1 = sentence.codePointAt(1); // 97
console.log(`Unicode Code Point of '${sentence.charAt(1)}': ${codePt1}`);
for (let codePoint of sentence.slice(0,5)){
console.log(codePoint, codePoint.codePointAt(0));
}
let codePt3 = sentence.codePointAt(100);
console.log(codePt3); // undefined
Output
Unicode Code Point of 'a': 97 H 72 a 97 p 112 p 112 y 121 undefined
Recommended Reading: JavaScript String fromCodePoint()