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 ?
The main difference between the == and === operator in javascript is that the == operator does the type conversion of the operands before comparison, whereas the === operator compares the values as well as the data types of the operands.
Use === if you want to compare couple of things in JavaScript, it's called strict equality, it means this will return true if only both type and value are the same, so there wouldn't be any unwanted type correction for you, if you using == , you basically don't care about the type and in many cases you could face ...
It means null is equal to undefined but not identical. When we define a variable to undefined then we are trying to convey that the variable does not exist . When we define a variable to null then we are trying to convey that the variable is empty.
The === operator means "is exactly equal to," matching by both value and data type. The == operator means "is equal to," matching by value only.
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 fromType(y)
, return false.
2. IfType(x)
is Undefined, return true.
3. IfType(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
isnull
andy
isundefined
, return true.
3. Ifx
isundefined
andy
isnull
, return true.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With