I don’t understand why one scenario evaluates false and the other true.
Scenario 1:
> '' == ''
true
Scenario 2:
> '' == ('' || undefined)
false
Is scenario 2 not asking if (empty string) is equal to: (empty string) OR undefined?
I’m sure I’m missing something fundamental here, which is really what I’m trying to figure out. I can easily code around this, but I’d like to learn why it’s happening… for next time, ya know?
Is not the same as
It’s more along the lines of:
And then comparing
footo an empty string:Explanation
The logical
||is a short-circuiting operator. If the argument to its left is truthy, the argument to the right is not even evaluated. In JavaScript,''is not considered to be truthy:As such
undefinedis returned and compared against an empty string. When you perform this logic within a the parenthesis, the''and theundefineddon’t know about the impending equality check that is waiting to happen – they just want to know which of them is going to survive this evaluation.