Convert boolean result into number/integer Convert boolean result into number/integer javascript javascript

Convert boolean result into number/integer


Use the unary + operator, which converts its operand into a number.

+ true; // 1+ false; // 0

Note, of course, that you should still sanitise the data on the server side, because a user can send any data to your sever, no matter what the client-side code says.


Javascript has a ternary operator you could use:

var i = result ? 1 : 0;


Imho the best solution is:

fooBar | 0

This is used in asm.js to force integer type.