Javascript string comparison

I do not understand why one script evaluates to false and the other evaluates to true.

Scenario 1:

> '' == ''
true

Scenario 2:

> '' == ('' || undefined)
false

Is script 2 set if (empty string) is: (empty string) OR undefined?

I am sure that something fundamental is missing here, and this is really what I am trying to understand. I can easily code this, but I would like to know why this happens ... next time, you know?

+5
source share
3 answers
'' == ( '' || undefined )

This is not the same as

( '' == '' ) || ( '' == undefined )

This is more on the lines:

var foo = '' || undefined; // Results in undefined

And then comparing foowith an empty string:

foo == ''; // undefined == '' will result in false

Explanation

|| . , . JavaScript '' :

if ( '' ) console.log( 'Empty Strings are True?' );

undefined . , '' undefined , - , .

+10

:

'' == ('' || undefined) // return "undefined"
'' == undefined // false

|| .

DEMO

:

'' == undefined  || '' == false

undefined == null, "":

  • 0
  • "" -
  • NaN
  • false
+2

'' == '' || '' == undefined

, . || - OR, .

+1
source

All Articles