things would be evaluate as true/false in if condition and the boolean operators ( && / || )
if (123) {
return true;
} else if (null) {
return false;
};
these values or types would be converted into false or truthy
Variable Type | when it is falsy | when it is truthy |
---|---|---|
boolean | false | true |
string | '' (empty string) |
any other string |
number | 0 NaN | any other number |
null | always | never |
undefined | always | never |
Any other Object including empty ones like {},[] | never | always |
use !!
The first ! convert a value into boolean type but inverts the logic. The second one toggles it again to mathc the nature of the original obkect
// Direct variables
const hasName = !!name;
// As members of objects
const someObj = {
hasName: !!name
}
// e.g. in ReactJS JSX
{!!someName && <div>{someName}</div>}