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
let trueBool: boolean = true;
let falseBool: boolean = false;
console.log(+trueBool) // 1
console.log(+falseBool) // 0
Output
The
+ unary operatorreturn0forfalseand1fortrue.
Thanks for reading…