Casting type when == is used instead of ===

I usually don't prefer to use ==, but today I just experimented with the following code, including ==, and the results are a little confusing to me. Can someone explain what is happening?

All of these values ​​are false:

'', 0, false, undefined, null 

Suppose I did:

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

The statements below are true:

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

But why does the code below return false?

 undefined == 0 undefined == '' null == 0 
+6
source share
4 answers

Since 0 , false and '' are “values”, and null is “no value” and undefined is “no definition”.

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

+3
source

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

undefined == 0 and undefined == '' :

According to the specification, undefined compares true only with undefined or null . Since neither 0 nor '' are none of these values, it returns false.

null == 0

According to the specification, null compares only the truth with another null or with undefined - here and not here.

Use ===

Generally, it's better to use === and do the comparison that you really intend, instead of trusting the == algorithm, and usually faster. It is easy to be caught, as you saw when the algorithm does not do what you expect, and it is usually easier to be explicit than to keep track of which path the comparison algorithm will follow.

For general "sameness" in javascript, see this MDN page .

0
source

null and undefined are nonexistent. i.e.

 var a; var b = null; 

Here a and b have no meaning. Whereas: 0 , false and '' are all values. One thing that is common between everyone is that they are all meanings of falsity, which means that everyone satisfies the conditions> .

So, 0 , false and '' together form a subgroup.

On the other hand, null and undefined form the second subgroup. Check out the comparisons below. null and undefined. the other three are equal to each other. But all of them are considered as false conditions in JS.

enter image description here

This is the same as any object (for example, {} , arrays , etc.), a non-empty string, and Boolean true are all true conditions. But they are not all equal.

0
source

But why does below give false?

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

The technical "Why" is because it says the truth table.

The philosophical “why” is due to the fact that undefined and null are not a number and are not a number string.

( parseInt(undefined) -> NaN ).

So NaN != 0 .

But be careful: also undefined != NaN , because NaN != NaN . Just because a banana is not a kiwi. But also Apple is not a qiwi. Still, a banana is not Apple.

So the avetisk answer is correct. You cannot compare an object that you know that they are NOT, but you do not know what it is.

-1
source

All Articles