This is because the Abstract Equality Comparison Algorithm requires that if Type(x) or Type(y) is a Boolean in the expression x == y then the Boolean value should be coerced to a number via ToNumber, which converts true to 1 and false to +0.
This means that any comparison of true == something or something == true results in 1 == something or something == 1 (replacing true and 1 with false and +0 for false).
The Null type does not compare as equal to either 1 or +0 (in fact, null is only comparable to undefined in the Abstract Equality Comparison Algorithm).
There is a detailed discussion of all of the different kinds of equality in JavaScript on MDN that is well worth looking at if you want to know more.
However, if you coerce null to a number it is coerced to +0 so +null == false actually returns true.
This is because the Abstract Equality Comparison Algorithm requires that if Type(x) or Type(y) is a Boolean in the expression x == y then the Boolean value should be coerced to a number via ToNumber, which converts true to 1 and false to +0.
This means that any comparison of true == something or something == true results in 1 == something or something == 1 (replacing true and 1 with false and +0 for false).
The Null type does not compare as equal to either 1 or +0 (in fact, null is only comparable to undefined in the Abstract Equality Comparison Algorithm).
There is a detailed discussion of all of the different kinds of equality in JavaScript on MDN that is well worth looking at if you want to know more.
However, if you coerce null to a number it is coerced to +0 so +null == false actually returns true.
Adding to the current discussion. null >= false returns true.
I believe that it is because this is interpreted as !(null < false)
Videos
What is a null check?
What is a strict null check?
In programming, truthiness or falsiness is that quality of those boolean expressions which don't resolve to an actual boolean value, but which nevertheless get interpreted as a boolean result.
In the case of C, any expression that evaluates to zero is interpreted to be false. In Javascript, the expression value in
if(value) {
}
will evaluate to true if value is not:
null
undefined
NaN
empty string ("")
0
false
See Also
Is there a standard function to check for null, undefined, or blank variables in JavaScript?
The set of "truthy" and "falsey" values in JavaScript comes from the ToBoolean abstract operation defined in the ECMAScript spec, which is used when coercing a value to a boolean:
+--------------------------------------------------------------------------+
| Argument Type | Result |
|---------------+----------------------------------------------------------|
| Undefined | false |
|---------------+----------------------------------------------------------|
| Null | false |
|---------------+----------------------------------------------------------|
| Boolean | The result equals the input argument (no conversion). |
|---------------+----------------------------------------------------------|
| Number | The result is false if the argument is +0, −0, or NaN; |
| | otherwise the result is true. |
|---------------+----------------------------------------------------------|
| String | The result is false if the argument is the empty String |
| | (its length is zero); otherwise the result is true. |
|---------------+----------------------------------------------------------|
| Object | true |
+--------------------------------------------------------------------------+
From this table, we can see that null and undefined are both coerced to false in a boolean context. However, your fields.length === 0 does not map generally onto a false value. If fields.length is a string, then it will be treated as false (because a zero-length string is false), but if it is an object (including an array) it will coerce to true.
If fields should be a string, then !fields is a sufficient predicate. If fields is an array, your best check might be:
if (!fields || fields.length === 0)
Well, you can always "give up" :)
function b(val){
return (val==null || val===false);
}
I think what you're looking for is !!val==false which can be turned to !val (even shorter):
You see:
function checkValue(value) {
console.log(!!value);
}
checkValue(); // false
checkValue(null); // false
checkValue(undefined); // false
checkValue(false); // false
checkValue(""); // false
checkValue(true); // true
checkValue({}); // true
checkValue("any string"); // true
That works by flipping the value by using the ! operator.
If you flip null once for example like so :
console.log(!null) // that would output --> true
If you flip it twice like so :
console.log(!!null) // that would output --> false
Same with undefined or false.
Your code:
if(val==null || val===false){
;
}
would then become:
if(!val) {
;
}
That would work for all cases even when there's a string but it's length is zero.
Now if you want it to also work for the number 0 (which would become false if it was double flipped) then your if would become:
if(!val && val !== 0) {
// code runs only when val == null, undefined, false, or empty string ""
}
If the three possible inputs are only null, true and false, you can use
return nullableBool !== false
but explicitly listing the matching values isn't bad for readability either
return v === null || v === true
Using === instead of == isn't actually necessary here, as null != false (whereas Boolean(null) == false), but it might help for clarity if you're not familiar with the loose equality rules.
The elegant solution to is null or true is to use nullish coalescing operator
return nullableBool ?? true
It will return true if nullableBool is either null or undefined. Otherwise, it will return the value of nullableBool. If you only want to return true or false, then you can use double bang:
return !!(nullableBool ?? true)
Now, it will return true if nullableBool is either null, undefined or truthy value. Otherwise, return false.
I am looking for 1 name to describe a value that could be true, false, null, or undefined and I think Boolean is close but perhaps not technically correct and it made me wonder if there was a better name.