6

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?

1
  • 2
    your question is just like saying that 2 == 3 || 2 is testing if 2 equals 3 or 2. If you really stop to think about it as a computer would read it, you can see that it doesn't make sense Commented May 21, 2012 at 19:29

3 Answers 3

12
'' == ( '' || 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.

Sign up to request clarification or add additional context in comments.

1 Comment

"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" That was what made it click for me. Thanks very much @JonathanSampson
2

Let's break it:

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

|| return the first true value or the last value.

DEMO

You want this:

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

undefined is == only to null, and not to all other "falsy" values:

  • 0
  • "" - empty string
  • NaN
  • false

1 Comment

"|| return the first true value or the last value." - that made me understand how that operator works and why (a || a === 0) is false and (a === 0 || a) is "" when a = "". thanks!
1

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

As in almost all programming languages, the expressions at both sides of an operator must evaluate to a valid operand. || is logical OR operator which is used to evaluate a pair of boolean operands.

5 Comments

what do you mean by "|| is logical OR which requires a pair of boolean expressions"
|| doesn't require to boolean expressions!
@gdoron boolean expression is one that results a value of true or false. (true || false) is an expression as well as ((true && false)||(false||false)). In both cases you require a boolean expression on each side of the logical operator.
But it's not true... you can write var x = 0 || '' || 3|| null; alert(x) // 3
@gdoron in JS anything can evaluate to a Boolean value.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.