As far as I know expressions are not executed after falsy values in Javascript. For example in the following statement:
const result = undefined && 5;
console.log(result);
result
will be undefined.
However:
const result = false && false ? 'T' : 'F';
console.log(result);
result
will be equal to F
. Why is the ternary expression still executed?
This is because of operator precedence: &&
has higher precedence (6) than ? :
(4), so
false && false ? 'T' : 'F'
evaluates to
(false && false) ? 'T' : 'F'
So, the left-hand side evaluates to false
first (taking the first false
), and then goes on to the conditional operator.
If you had put parentheses after the &&
, result
would be false
, as you're expecting:
const result = false && (false ? 'T' : 'F');
console.log(result);
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