Check for bool in JavaScript

I have the following jQuery (I have it wrapped in a document ready function and all that, so please know that 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 False value of the uppercase F when it hits the warning, but never enters my next if statement. I know this must be something stupidly simple.

+4
source share
2 answers

If the input value contains the string "False", this will not result in a false boolean value. You will need to really check itemIsSold == "False" .

+8
source

Since the value of the hidden input field is a string,! !"False" will be evaluated as false. Note that any string other than a string with a length of 0 is considered true. Therefore, it is better to compare the string value with another string value, for example "False" :

 if (itemIsSold == "False") { // … } 
+4
source

All Articles