Categories
JavaScript Answers

How to Convert a Boolean to an Integer with JavaScript?

Spread the love

Sometimes, we want to convert a boolean to an integer with JavaScript.

In this article, we’ll look at how to convert a boolean to an integer with JavaScript.

Convert a Boolean to an Integer with JavaScript

To convert a boolean to an integer with JavaScript, we can use the Number function.

For instance, we write:

console.log(Number(true))  
console.log(Number(false))

then we get 1 and 0 in the console log respectively.

We just pass true and false as arguments into the Number function to do the conversion.

Conclusion

To convert a boolean to an integer with JavaScript, we can use the Number 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 *