JohnN
JohnN

Reputation: 1010

If statements to check that variable is a boolean results in "boolean != boolean"

I'm attempting to put in some error handing into some of my functions that check whether or not a particular field is enabled or not. I wanted to add some conditional checks to make sure that a particular parameter of the function was always a boolean value (i.e true to verify the field is enabled / false to verify the field is disabled) because I noticed that my functions would not yield errors if a different data type was used.

So I set up a little test code that would also allow the user to use certain strings in place of true boolean values. That looks something like this: (Note: We're using Squish for GUI testing and using some of their in-house functions)

function boolCheck(bool)
{
    var truth = ["true","True","TRUE","T","t"];
    var lies = ["false","False","FALSE","F","f"];

    if (truth.indexOf(bool) !== -1)
    {
        bool = true;
    }
    if (lies.indexOf(bool) !== -1)
    {
        bool = false;
    }
    else if (bool !== true || bool !== false)
    {
        test.fatal("ERROR: " + (typeof bool).toUpperCase() + " is not a BOOLEAN data type.");
    }
}


function main()
{
    boolCheck(true);
}

Running this presents the following result in what is essentially the console (test.[something] is basically like printing to the console)

Fatal ERROR: BOOLEAN is not a BOOLEAN data type.

That seems, odd to me.

Further, running this in main:

function main()
{
    boolCheck("T")
}

gives this result:

Fatal ERROR: BOOLEAN is not a BOOLEAN data type.

Obviously, there's something wrong with how this is all being handled. Maybe I'm just going about this all the wrong way. Can anyone offer some insight as to why "BOOLEAN" and "BOOLEAN" are not showing up as equal? Or a better way to handle this period? For example, I'd love to think of a good way to wrap this all in a Try/Catch.

Upvotes: 0

Views: 1038

Answers (2)

Neel Rathod
Neel Rathod

Reputation: 2111

Why you not use npm module boolean

It's match with your requirement

    const boolean = require('boolean');

    console.log(boolean('t')) // true
    console.log(boolean('True')) // true
    console.log(boolean(true)) // true
    console.log(boolean('TrUe')) // true
    console.log(boolean('TRUE')) // true
    console.log(boolean('true')) // true

    console.log(boolean('f')) // false
    console.log(boolean('False')) // false
    console.log(boolean(false)) // false
    console.log(boolean('FaLsE')) // false
    console.log(boolean('FALSE')) // false
    console.log(boolean('false')) // false

Upvotes: 0

epascarello
epascarello

Reputation: 207531

Simple logic error

if (bool !== true || bool !== false)

So lets read it out loud. If bool is not equal to true or bool is not equal to false, go into this block.

So when bool is true it is not false so it will go into the if block.

It needs to be if bool is not euqal to true AND bool is not equal to false

Upvotes: 2

Related Questions