How to get hex integer from a string in JavaScript?

Sometimes, we want to get hex integer from a string in JavaScript.

In this article, we’ll look at how to get hex integer from a string in JavaScript.

How to get hex integer from a string in JavaScript?

To get hex integer from a string in JavaScript, we can use the parseInt function.

For instance, we write:

const str = "#FFFFFF"
const hex = parseInt(str.replace(/^#/, ''), 16);
console.log(hex)

We call str.replace to remove the # sign with an empty string.

Then we call parseInt to convert the hex number to a decimal number.

As a result, we get 16777215 for the value of hex.

Conclusion

To get hex integer from a string in JavaScript, we can use the parseInt function.