Javascript Empty String Comparison
comparison, javascript
Solution
'' == ( '' || undefined )
Is not the same as
( '' == '' ) || ( '' == undefined )
It's more along the lines of:
var foo = '' || undefined; // Results in undefined
And then comparing `foo` to an empty string:
foo == ''; // undefined == '' will result in false
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:
if ( '' ) console.log( 'Empty Strings are True?' );
As such `undefined` is returned and compared against an empty string. When you perform this logic within a the parenthesis, the `''` and the `undefined` don'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.
Problem
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?