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.