1

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?

1 Answer 1

1

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));
}

Sign up to request clarification or add additional context in comments.

1 Comment

``` const icons = '$677y8'; console.log(icons.charCodeAt(icons[1])); /* for(let i = 0; i < icons.length; i++){ console.log(icons.charCodeAt(icons[i])); }*/``` returns NaN too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.