undefined === null => false
undefined == null => true
I have thought about the reason of undefined == null
and found out only one case:
if(document.getElementById() == null) ....
Is there any other reason to make (undefined === null) == false
?
Is there any other examples of use ===
- operation in javascript?
undefined is a primitive type of a variable which evaluates falsy, has a typeof() of undefined, and represents a variable that is declared but missing an initial value. null == undefined evaluates as true because they are loosely equal.
null is an assigned value. It means nothing. undefined typically means a variable has been declared but not defined yet. null and undefined are falsy values.
The undefined value is a primitive value used when a variable has not been assigned a value. The null value is a primitive value that represents the null, empty, or non-existent reference. When you declare a variable through var and do not give it a value, it will have the value undefined.
Null is an assignment value, which means that you can assign the value null to any variable when you want that variable to be empty. It is intentionally left blank and will point to an empty value. Undefined is a variable that exists but hasn't been initialized YET.
Is there any other reason to make (undefined === null) == false ?
They are not equal so the Strict Equality Comparison Algorithm considers them to be false.
Is there any other examples of use === - operation in javascript?
The ===
gives the most predictable result. I only use ==
when I have a specific purpose for type coercion. (See Abstract Equality Comparison Algorithm.)
null
and undefined
are two different concepts. undefined
is the lack of value (if you define a variable with var without initializing it, it doesn't contain null
, but undefined
), while with null
the variable exists and is initialized with the value null
, which is a special type of value.
JavaScript equality operator is broken though, Crockford found out that it lacks transitivity and for this reason suggests to use always the strict equality (===). Consider this table published in Javascript the good parts:
'' == '0' // false
0 == '' // true
0 == '0' // true
false == 'false' // false
false == '0' // true
false == undefined // false
false == null // false
null == undefined // true
===
is strict equal.
Undefined and null are not the same thing.
==
uses type coercion.
null
and undefined
coerce to each other.
Type coercion
(using the == operator) can lead to undesired or unexpected results. After following all the talks I could find of Douglas Crockford on the net (mostly Yahoo video) I got used to using ===
all te time. Given my default usage of the strict equal operator I would be more interested in type coercion javascript use cases ;~) nowadays.
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