5

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 : new Boolean(thatValue)

but it returns true even for "false" as a paremter

is there anyway to solve this? except me writing my own custom function that will return false if "flase" ?..

thank you

2 Answers 2

5

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";
Sign up to request clarification or add additional context in comments.

Comments

4

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.