Categories
JavaScript Answers

How to get hex integer from a string in JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *