Reputation: 120
The snippet below is from a larger codeblock of a project, the job of the snippet is to get the unicode values of each character and it works fine execpt from numbers 1 - 9 ( 0 works too ) which return undefined.
const icons = '$677y8';
for(let i = 0; i < icons.length; i++){
console.log(icons.codePointAt(icons[i]));
}
Also, i tried charCodeAt() method it returns NaN
const icons = '$677y8';
for(let i = 0; i < icons.length; i++){
console.log(icons.charCodeAt(icons[i]));
}
Is there another standard JavaScript method to get this done?
Upvotes: 1
Views: 84
Reputation: 16908
You have invoked it wrong, it is String.prototype.codePointAt(position)
.
So you need to pass the i
th index to the codePointAt
function as the position of the string to look for the Unicode value:
const icons = '$677y8';
for (let i = 0; i < icons.length; i++) {
console.log(icons.codePointAt(i));
}
btw, charCodeAt()
is UTF-16, where as codePointAt()
is Unicode.
//gets UTF-16 values
const icons = '$677y8';
for (let i = 0; i < icons.length; i++) {
console.log(icons.charCodeAt(i));
}
Upvotes: 1