Boolean to integer conversion

I have 3 separate Boolean variables, bit1, bit2 & bit3 and I need to calculate the decimal integer equivalent in JavaScript?


Solution 1:

+true //=> 1
+false //=> 0

+!true //=> 0
+!false //=> 1

Solution 2:

Ternary operator is a quick one line solution:

var intVal = bit1 ? 1 : 0;

If you're unfamiliar with the ternary operator, it takes the form

<boolean> ? <result if true> : <result if false>

From Sime Vidas in the comments,

var intVal = +bit1;

works just as well and is faster.