0

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?

8
  • 3
    use +!! operator like this +!!varname (it is three operators actually,+, !, !) Commented Mar 16, 2016 at 21:57
  • 2
    @BryanChen That is not cryptic enough. I am sure you can do better. Commented Mar 16, 2016 at 21:58
  • @BryanChen first time for me see such a thing... thanks! Commented Mar 16, 2016 at 22:05
  • If it's truthy (including true), it can't be null. Commented Mar 16, 2016 at 22:07
  • 1
    @SverriM.Olsen you need the ===+!!([!![]]+[]) synthetic operator if you want to just get true and not merely truthy. (Added this to my answer.) Commented Mar 16, 2016 at 22:08

3 Answers 3

4

Kind of a weird question since null is falsy.

return x === true; // not null and 'true';
return x; // return truthy value if x not null and truthy; falsy otherwise
return !!x; // return true if x not null and truthy, false otherwise
return +!!x; // return 1 if x not null and truthy, 0 otherwise 

!!x is same as !(!x) and casts x to true or false and negates, then negates a second time. Either a hack or a pattern that is same as Boolean(x) depending on your worldview.

+<boolean> will cast convert the boolean to the number 1 or 0.

Anyway someone requested a cryptic answer and "true" uses a lot of unnecessary characters so here it is:

return +!!(x === ([!![]]+[])); // 1 if x not null and true; 0 otherwise
Sign up to request clarification or add additional context in comments.

Comments

3

Simply with the strict equality operator (===):

The identity operator returns true if the operands are strictly equal [...] with no type conversion.

function checkNotNullAndTrue(v) {
    return v === true ? 1 : 0;
}

or

function checkNotNullAndTrue(v) {
    return +(v === true);
}

Why hacky stuff does not work, sometimes:

// djechlin's part
write(+!!1);                           // 1 obviously not true
write(+!![]);                          // 1 obviously not true

// quentin's part
function test (valid_variable_name) {
    if (valid_variable_name) {
        return 1;
    }
    return 0;
}

write(test(1));                        // 1 obviously not true
write(test([]));                       // 1 obviously not true

// my part
var v = true;
write(+(v === true));                  // 1 true (the only one, that would work!)
write(+(1 === true));                  // 0 false, works
write(+([] === true));                 // 0 false, works


function write(x) {
    document.write(x + '<br>');
}

Comments

2

Since null (and undefined, which you mention in your examples) are not true values, testing for them is redundant.

if (valid_variable_name) {
    return 1;
}
return 0;

… is sufficient.

… or if (valid_variable_name === true) if you want to test for true rather than any true value.

1 Comment

but on 1, you get 1 (true), it should return 0 (false).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.