PositiveGuy
PositiveGuy

Reputation: 47743

Check for bool in JavaScript

I've got the following jQuery (I've got it wrapped in the document ready function and all that, so please know I'm just showing you the inside of the function.

..
            var itemIsSold = $("#itemIsSold").val();
            alert(itemIsSold);
            if(!itemIsSold) {
               ...
    }

where itemIsSold is a hidden input field. I get the value False upper case F when it hits the alert but never enters my next if statement. I know this has to be something stupid simple.

Upvotes: 4

Views: 14556

Answers (2)

Rudism
Rudism

Reputation: 1655

If the input's value contains the string "False", that will not translate into a false boolean value. You will need to actually check for itemIsSold == "False".

Upvotes: 8

Gumbo
Gumbo

Reputation: 655189

Since the value of the hidden input field is a string, !"False" will be evaluated to false. Note that any string other than a string with the length of 0 is treated as true. So you should rather compare the string value to another string value like "False":

if (itemIsSold == "False") {
    // …
}

Upvotes: 4

Related Questions