Shubhangi Garg Shubhangi Garg - 4 months ago 13
HTML Question

Javascript is not detectinng boolean false

I have one variable in my javascript file. Lets call that variable "isValid". Its value is false.
I have to check its value in if statement , like this:

if(!isValid){
//Do something
}


But its not at all going inside if statement. I have checked this by putting alert statement. Then I have used typeof() like this:

if(typeof(isValid) === false){
//Do something
}


But this is also not working. Any suggestion on this?

Answer

check this : if variable contains false just give condition value == 'false'

var value = "false"; //string

if (value=='false')
{
    console.log("this is false")
}
else
{
   .... some code
}

value = false; //now your variable is a boolean
if(value === false)
{
    return "something";
}
else
{
    ..... some code
}

// print : this is false