When I am using any one of values(null
, undefined
, false
, ''
, 0
) in a if
statement, it is always evaluated as fallacy(false
). Also, the negation of these values((null
, undefined
, false
, ''
, 0
) in a if
statement always evaluated as tautology(true
).
if(null){
}else{
}
if(undefined){
}else{
}
if(false){
}else{
}
if(''){
}else{
}
if(0){
}else{
}
In all the above cases, if
statement is evaluated as false
& else
statement executes.
However, when I am comparing these fallacy values with ==
operator, it is not returning true
always. Surprisingly, it is always returning true
values when I am comparing the negation of these values.
if double equalto (==
) operator checks/compares for values & not strictly for types, then why:
null == false // returns false
null == 0 // returns false
null == '' // returns false
But,
!null == !false // returns true
!null == !0 // returns true
!false == !undefined // returns true
And,
null == undefined // returns true
false == 0 // returns true
I appreciate if any one can clarify the behavior or relationship among these values(null
, undefined
, false
, ''
, 0
).
There are only six falsey values in JavaScript: undefined , null , NaN , 0 , "" (empty string), and false of course.
Empty strings are "falsy" which means they are considered false in a Boolean context, so you can just use not string.
Both undefined and null are falsy by default. So == returns true. But when we use the strict equality operator (===) which checks both type and value, since undefined and null are of different types (from the typeof Operator section), the strict equality operator returns false.
Say, if a string is empty var name = "" then console. log(! name) returns true . this function will return true if val is empty, null, undefined, false, the number 0 or NaN.
"...If double equalto (
==
) operator only checks/compares for values & not for types..."
That's an incorrect assumption, though it's often repeated by people. In reality, the ==
does check types, and in fact pays far more attention to the types than a ===
comparison does.
See Abstract Equality Comparison Algorithm.
A ==
comparison doesn't do a simple toBoolean conversion. Rather it walks through a somewhat complex recursive algorithm, which, after checking the types, attempts to coerce the operands to the same type if they don't match.
The type coercion that it performs is very specific to the types of the operands. A different sequence of coercions can take place for different type pairs. Usually (but not always) it ends up ultimately coercing the operands down to number
types.
!
ing the operands changes thingsWhen you manually coerce both operands using !
, you're now doing a simple toBoolean conversion causing the types to match, which avoids the type coercive part of the algorithm, making it behave essentially like the Strict Equality Comparison Algorithm.
So the only way to predict the outcome of a ==
comparison when the types don't match is to understand that Abstract algorithm.
NaN
And FYI, there's one more "falsey" value to consider, NaN
. Its ==
comparison will always be false
, no matter what. Even when comparing to another NaN
value, it'll be false
.
undefined
: means a variable was declared but has no value assigned
null
: the value of null has been assigned, which means it has no value
false
, ''
and 0
I think you can probably work out what these mean.
NULL is different from false (NULL is of type object and false is of type of boolean), null is different from 0 (0 is of type integer), null is also different from '' ('' is of type of string). but they are all falsy values. The ! operator negates a boolean value. If ! is used on falsy values, it results to conversion of falsy values to an object of type boolean.
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