Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on In javascript is there really a good reason to never check for boolean true || false such as if(var){}else{}?

Parent

In javascript is there really a good reason to never check for boolean true || false such as if(var){}else{}?

+1
−0

I am fairly certain this isn't a duplicate so please bear with me.

I check for boolean true||false using if() as a matter of course in my programming. I've programmed extensively in PHP, some in C# ASP.NET, a bit in Java, a long time ago in C++, & dabled in a few others. Checking for boolean true||false has always been pretty straightforward (as far as I could tell). But in JavaScript I've heard, read, and otherwise been told it's bad. That instead of:

if(var){}else{}

I should instead do:

if(typeof(var) !== 'undefined' || typeof(var) !== null || var !== ''){}else{}

Previous to the six months prior to asking this question (originally on StackOverflow) I was a dabbler in JavaScript. After getting tired of writing & re-writing the long version of the boolean test shown above I finally asked a friend who's done extensive js development for years. My friend supported what I'd read, that I should never test for boolean true or false the way I'm used to. However, after that discussion I have a stronger belief that if(var){}else{} IS actually completely fine in js as it works EXACTLY like I would intuitively expect it to (my jsfiddle testing this)

I've looked around and found various links. The following seemed to be the more relevant:

The thing that convinced me most that my usage is safe and will work fine is the 3rd answer to the first SO question I linked to above given by Incognito. The js spec is very clear about what will & will not evaluate to boolean true||false, and again this is exactly as I would have expected (though have to be reminded that an empty array is an object... but that is specific to JavaScript, while the rest of it is exactly as I would expect).

Can someone please provide a definitive reason to not check for boolean true or false in JavaScrpt, realizing I know the difference between a boolean check and an equality check??

Thanks in advance!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

Post
+0
−0

As mentioned in another answer, when you have code such as if (someVar), you're subject to truthiness. To be more precise, this code will run under the rules of Boolean coercion.

In JavaScript, when you have if (someVar), the variable someVar can be basically "anything" - even a boolean value! If someVar is not a boolean, it'll be converted according to some rules:

  • Booleans are returned as-is.
  • undefined turns into false.
  • null turns into false.
  • 0, -0, and NaN turn into false; other numbers turn into true.
  • 0n turns into false; other BigInts turn into true.
  • The empty string "" turns into false; other strings turn into true.
  • Symbols turn into true.
  • All objects become true.

A little test to check this behaviour:

function test(someVar) {
    if (someVar) {
        console.log(someVar, typeof someVar, 'true');
    } else {
        console.log(someVar, typeof someVar, 'false');
    }
}

for (const v of [ null, undefined, 0, 1, '', 'abc', [], [1, 2], {} ]) {
    test(v);
}

Output:

null object false
undefined undefined false
0 number false
1 number true
 string false
abc string true
[] object true
[ 1, 2 ] object true
{} object true

Note that any object is type-coerced to true1 (even an empty array or empty object).

  1: Except, of course, null (which is an object - although it shouldn't - but it's coerced to false)


Now back to the question: how should I check for a boolean value?

It depends!

If you know that someVar is a boolean value (either true or false, nothing else, and that's guaranteed in another part of the code), then if (someVar) is the most straighforward way to check it. You don't need anything else to verify if the value is true or false.

On the other hand, if you don't know what type someVar is (or you know, and it can be either a boolean or some other type), and you want to check if the type is boolean, then you could do something like this:

function checkBoolean(someVar) {
    if (typeof someVar === 'boolean' && someVar) {
        console.log('true');
    } else {
        console.log('false or not boolean');
    }
}

But in this case, if someVar is false, it'll enter the else clause (it's a boolean, but it's not true). If you want to differentiate between true, false and "anything else", then it should be:

function checkBoolean(someVar) {
    if (typeof someVar === 'boolean') { // it's a boolean value
        // it's either true or false
        if (someVar) {
            console.log('true');
        } else {
            console.log('false');
        }
    } else { // it's not a boolean
        console.log('not boolean');
    }
}

But if you don't care about the type of someVar and just want to know if it's "true" or "false" (according to the language's type coercion rules), if (someVar) is enough.

And of course, if you need a different check (such as "I want empty arrays to be false"), then just change the clause accordingly (ex: if (Array.isArray(someVar) ? someVar.length > 0 : someVar) - if it's an array, it can't be empty, otherwise check for truthiness).

Anyway, you must define exactly what you need: check if the type is boolean, or if the type-coerced value is true/false, or anything else (ignore null and undefined, or empty arrays can't be true, etc). Then you change the code accordingly.


Regarding the suggested code

IMO, if (typeof(someVar) !== 'undefined' || typeof(someVar) !== null || someVar !== '') is not a good way to check boolean values. Actually, I believe there's an error in this code. For example, if someVar is null, it enters the if clause:

const someVar = null;
if (typeof(someVar) !== 'undefined' || typeof(someVar) !== null || someVar !== '') {
    console.log('ok');
} else {
    console.log('not ok');
}

This code prints "ok", because typeof(null) is not null.

I believe that typeof(someVar) !== null is wrong, because it doesn't make sense: typeof anything will never be null (according to the language specification, the result of typeof is always a string).

Actually, it's worse: even if someVar is undefined or the empty string, this code will print "ok". That's because typeof(someVar) is never null, thus the condition typeof(someVar) !== null is always true. Therefore, this code is not checking anything.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Even if you replace `typeof someVar !== null` with `someVar !== null`, it still won't work. The condi... (1 comment)
Even if you replace `typeof someVar !== null` with `someVar !== null`, it still won't work. The condi...
Andrew Ray‭ wrote 6 months ago

Even if you replace typeof someVar !== null with someVar !== null, it still won't work. The condition typeof someVar !== "undefined" || someVar !== null is always true because typeof someVar is only "undefined" if someVar is undefined, in which case, it's not null; conversely, if someVar is null, then typeof someVar is "object" and not "undefined".