Skip to content Skip to sidebar Skip to footer

Boolean Object In Javascript Returns True For "false" Parameter

I have a little problem. I have situations where my ajax calss returns a string. sometimes that string is 'false' i want to always convert that string value into a boolean i tried

Solution 1:

The best way to do this you've already described:

if(value === 'true') {
  //do something
}

Or:

if(value !== 'false') {
  //do something
}

You're limited by JavaScript's weak typing here, actually working to your disadvantage, where any non-empty string will convert to a true boolean, even if that string is "false".

To get it and store it for use elsewhere, something like this works:

var myBool = value !== "false";

Solution 2:

A string is always true if it contains some text, even if that text is "false". You can check for it using the ternary operator:

thatValue == "false" ? false : true

Post a Comment for "Boolean Object In Javascript Returns True For "false" Parameter"