6

I generally don't prefer using == but today i was just experimenting with the following code including == and the results are a bit confusing to me. Can someone please explain what is happening?

All these are falsy values:

'', 0, false, undefined, null

Suppose i did:

if (undefined == null) {
    alert('a');
} else {
    alert('b');
}

The statements below give true:

null == undefined
0 == ''
false == ''
0 == false

But why does the code below return false?

undefined == 0
undefined == ''
null == 0
Maria Ines Parnisari
  • 16,584
  • 9
  • 85
  • 130
AMY
  • 248
  • 1
  • 10

4 Answers4

3

Because 0, false and '' are "values" while null is "absence of value" and undefined is "absence of definition".

Thus, you can compare "values" with each other, same for "non-values". But you can't compare "values" with "non-values".

avetisk
  • 11,651
  • 4
  • 24
  • 37
  • 2
    There is a specific algorithm for the `==` operator, and it is not so simple as value-ness or truthiness or any other generalisation. – Phil H Apr 14 '14 at 10:00
  • 1
    Well, while everybody (including myself) is aware of the algorithm (cf. all the comparison tables and ECMAScript specs.), providing the algorithm doesn't answer the question: WHY. – avetisk Apr 14 '14 at 10:58
  • Ok, I interpreted the question as 'why do I get these values', not 'why is the spec like this'. Not everybody is aware of the algorithm; most js developers use `==` until something like this confuses them, then they come to SO for answers. – Phil H Apr 14 '14 at 11:11
0

The == operator does not just check whether things are falsy; it follows a procedure called the The Abstract Equality Comparison Algorithm, which can be seen here: ECMAScript spec.

undefined == 0 and undefined == '':

According to the spec, undefined only compares true with undefined or null. Since neither 0 or '' are either of those values, it returns false.

null == 0

According to the spec, null only compares true with another null or with undefined - neither is the case here.

Use ===

It is generally better to use === and make the comparison you actually intend, rather than trusting to the == algorithm, and is generally faster too. It is easy to be caught out, as you have seen, when the algorithm does not do what you are expecting, and it is generally simpler to be explicit than to keep track of which path the comparison algorithm will take.

For general information on 'sameness' in javascript, see this MDN page.

Phil H
  • 19,928
  • 7
  • 68
  • 105
0

null and undefined are nothingness. i.e.

var a;
var b = null;

Here a and b do not have values. Whereas: 0,false and '' are all values.One thing common beween all these are that they are all falsy values, which means they all satisfy falsy conditions.

So, the 0,false and '' together form a sub-group.

On the other hand, null & undefined form the second sub-group. Check the comparisons in below image. null and undefined would equal. the other three would equal to each other. But, they all are treated as falsy conditions in JS.

enter image description here

This is same as any object(like {},arrays etc.), non-empty string & Boolean true are all truthy conditions. But, they are all not equal.

Liam
  • 27,717
  • 28
  • 128
  • 190
vivek_nk
  • 1,590
  • 16
  • 27
-1

But why does below gives false?

undefined == 0
undefined == ''
null == 0

The technical “Why” is because the Truth Table says this.

The philosophical “why” is because undefined and null are not a number and are not a string.

(parseInt(undefined) -> NaN).

So NaN != 0.

But careful: also undefined != NaN because NaN != NaN. Simply because a Banana is not a Kiwi. But Also Apple is not a Kiwi. And still Banana is not an Apple.

So the answer of avetisk is correct. You cannot compare object that you know what DON'T they are, but you don't know what they are.

Luca Rainone
  • 16,138
  • 2
  • 38
  • 52
  • Consider modifying your Banana/Kiwi/Apple comparison; NaN compares false with everything, including itself, because the IEEE754 spec says so, and javascript uses IEEE754 doubles for all numeric values. – Phil H Apr 14 '14 at 10:02
  • @PhilH exactly what I said. Banana and Kiwi are not equals, still if both are not Apple. I said explicitly that `NaN` is `!=` of `NaN` – Luca Rainone Apr 14 '14 at 11:32
  • Ok, well the logic is not really complete, as A != B && C != B does not imply B != B, but NaN != NaN is what I am pointing out. Even the same NaN... if someVal is a nan, even someVal == someVal returns false. By comparison, a kiwi is a kiwi. – Phil H Apr 14 '14 at 13:12
  • No. By comparison: a "not kiwi" is not equal to another "not kiwi" :-). But anyway, it was just an example, and the examples are never 1:1. – Luca Rainone Apr 14 '14 at 19:53