19

Can someone help me by explaining the difference. From what I understand the === does an exact match but what does this mean when comparing with null ?

Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427

5 Answers5

24

What does this mean when comparing with null?

It means exactly what you already said: It checks whether the value is exactly null.


a === null is true if the value of a is null.

See The Strict Equality Comparison Algorithm in the specification:

1. If Type(x) is different from Type(y), return false.
2. If Type(x) is Undefined, return true.
3. If Type(x) is Null, return true.

So, only if Type(a) is Null, the comparison returns true.

Important: Don't confuse the internal Type function with the typeof operator. typeof null would actually return the string "object", which is more confusing than helping.


a == null is true if the value of a is null or undefined.

See The Abstract Equality Comparison Algorithm in the specification:

2. If x is null and y is undefined, return true.
3. If x is undefined and y is null, return true.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
0

=== means it checks both the value and the type of the variables. For example pulled from the w3c page, given x = 5, x is an int, so x==="5" is false because it is comparing and int to string and x ===5 is true because it is both an int and the right value.

azealin
  • 11
0

=== is strict operator it not only compare value, but also type of variables so

string===string
int===int

== only compare values.

Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
0

Using the triple equals, the values must be equal in type as well.But not in ==.

i.e

1==true // this return true
1===true // but return false

a==null // will true if a is null or undefined
Vivek S
  • 2,010
  • 1
  • 13
  • 15
-1

1==true will be true

but 1===true will be false

eg. === compares on data type level while using == JavaScript will typecast by it self

Victor Häggqvist
  • 4,484
  • 3
  • 27
  • 35