Hello Friends 👋,
Welcome To Infinitbility! ❤️
Today, we will know the easiest way to convert boolean to number in typescript.
TypeScript has + unary operator
which helps you to convert boolean to the number when we add a unary operator with true
it will return 1
, and with false
it will return 0
.
Take an example
1let trueBool: boolean = true;2let falseBool: boolean = false;34console.log(+trueBool) // 15console.log(+falseBool) // 0
Output

The
+ unary operator
return0
forfalse
and1
fortrue
.
Thanks for reading…
Follow me on Twitter
Join our email list and get notified about new content
No worries, I respect your privacy and I will never abuse your email.
Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.