Why does 1 && true get a bool value true, but 0 && true get a number 0?
I tested it in Chrome Console and Firebug.
Because expr1 && expr2 will return expr1 if it is false, otherwise it will return expr2.
Because that is how logical operators in Javascript are defined to behave.
Additional reference: ECMA 262, page 83.
The && operator is commonly called logical and. If the first operand is false, null, undefined, "" or the number 0 then it returns the first operand. Otherwise, it returns the second operand
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With