I've known null
to be falsy
. Then why does it act as if it is a truthy
?
var status = null;
console.log('status:', status);
if(!!status) {
console.log('status is truthy'); // it should not print
}
if(!!null) {
console.log('null is truthy'); // it should not print
}
In JavaScript, a truthy value is a value that is considered true when encountered in a Boolean context. All values are truthy unless they are defined as falsy. That is, all values are truthy except false , 0 , -0 , 0n , "" , null , undefined , and NaN .
Answer : There no relative aspect between null and boolean. The value null is a literal (not a property of the global object like undefined can be). In APIs, null is often retrieved in place where an object can be expected but no object is relevant.
2. How to check for null. missingObject === null evaluates to true because missingObject variable contains a null value. If the variable contains a non-null value, like an object, the expression existingObject === null evaluates to false .
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.
The issue is there is already a window.status
, with which you conflict. It has setters that always make it a string, which causes your problems.
Change variable name status
to something else (like status1
) and the problem vanishes. This happens due to conflict with status
object property of windows.
var status1 = null;
console.log('status1 -->', status1)
if(!!status1) {
console.log('status') // it should not print
}
if(!!null) {
console.log('null') // it should not print
}
NOTE: No matter what value you assign to window.status
it'll get converted back to string. See this:
console.log(typeof window.status)
window.status = 4; // type Number
console.log(typeof window.status) // still it remains string
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