What is the best way to check if a javascript variable is both not null and true?
So for example, let’s say I have the below code:
var trueVar = true;
var falseVar = false;
function checkNotNullAndTrue(someVar) {
if (someVar != null && someVar) {
return 1;
} else {
return 0;
}
}
checkNotNullAndTrue(trueVar) should return 1.
checkNotNullAndTrue(falseVar) should return 0.
checkNotNullAndTrue(someUndefinedVariable) should also return 0.
Is this the best way to do this or is there a better way?
+!!operator like this+!!varname(it is three operators actually,+,!,!)true), it can't benull.===+!!([!![]]+[])synthetic operator if you want to just gettrueand not merely truthy. (Added this to my answer.)