admin管理员组

文章数量:1278986

I have a result ing from PHP side and json_encode send all the values as string (not as it's original type). The string ing at quote_tax is a 1|0 and of course this is true|false for example:

{
  "country_id": "3",
  "country_name": "Italy",
  "contract_tax": "1",
  "quote_tax": "0",
  "contract_inflation": "0",
  "quote_inflation": "0"
}

When I want to perform some operation based on such values and because they are ing as string I need to do something like this:

 data.quote_tax == '1' 
                ? $('#contract_tax').val(1).attr('checked', 'checked') 
                : $('#contract_tax').val(0).removeAttr('checked');

I did know about .parseInt() to convert them into a integer but I believe then I would need to have the same parison but this case paring with INT:

 data.quote_tax == 1 
                ? $('#contract_tax').val(1).attr('checked', 'checked') 
                : $('#contract_tax').val(0).removeAttr('checked');

I have tried this way:

 Boolean(data.quote_tax) == true 
                ? $('#contract_tax').val(1).attr('checked', 'checked') 
                : $('#contract_tax').val(0).removeAttr('checked');

And it doesn't work since Boolean(data.quote_tax) always evaluate as true even if data.quote_tax = '0'.

I have check this posts already but I couldn't found a proper solution:

  • How can I convert a string to boolean in JavaScript?
  • Convert string to Boolean in javascript

Any ideas what's wrong here or how to do this?

I have a result ing from PHP side and json_encode send all the values as string (not as it's original type). The string ing at quote_tax is a 1|0 and of course this is true|false for example:

{
  "country_id": "3",
  "country_name": "Italy",
  "contract_tax": "1",
  "quote_tax": "0",
  "contract_inflation": "0",
  "quote_inflation": "0"
}

When I want to perform some operation based on such values and because they are ing as string I need to do something like this:

 data.quote_tax == '1' 
                ? $('#contract_tax').val(1).attr('checked', 'checked') 
                : $('#contract_tax').val(0).removeAttr('checked');

I did know about .parseInt() to convert them into a integer but I believe then I would need to have the same parison but this case paring with INT:

 data.quote_tax == 1 
                ? $('#contract_tax').val(1).attr('checked', 'checked') 
                : $('#contract_tax').val(0).removeAttr('checked');

I have tried this way:

 Boolean(data.quote_tax) == true 
                ? $('#contract_tax').val(1).attr('checked', 'checked') 
                : $('#contract_tax').val(0).removeAttr('checked');

And it doesn't work since Boolean(data.quote_tax) always evaluate as true even if data.quote_tax = '0'.

I have check this posts already but I couldn't found a proper solution:

  • How can I convert a string to boolean in JavaScript?
  • Convert string to Boolean in javascript

Any ideas what's wrong here or how to do this?

Share Improve this question edited Nov 15, 2017 at 11:48 Liam 29.8k28 gold badges138 silver badges200 bronze badges asked Dec 9, 2016 at 15:51 ReynierPMReynierPM 18.7k55 gold badges204 silver badges387 bronze badges 1
  • "0" is truthy as it’s a non-empty string. "" and 0 are falsey. Try Boolean(Number(data.quote_tax)) == true or simply +data.quote_tax as the condition. – Sebastian Simon Commented Dec 9, 2016 at 15:53
Add a ment  | 

5 Answers 5

Reset to default 7

Values in Javascript are either "truthy" or "falsy", meaning they'll be interpreted as true or false in a boolean context. In the case of numbers, 0 is false and all others are true. If you convert your value to a number, you don't have to perform any other conversion to treat it as a boolean.

var x = parseInt('1');
x ? console.log(true) : console.log(false);

x = parseInt('0');
x ? console.log(true) : console.log(false);

See here for more information.

You can bine the operators - parse the string to a number, and then convert to Boolean:

console.log(Boolean(parseInt('0', 10)));

console.log(!!parseInt('0', 10));

console.log(!!+'0');

Your last approach fails because any non-empty string evaluates to true if converted to a boolean, including "0".


The integer 0 evaluates to false, while non-zero integers evaluate to true. For this reason, parseInt(data.quote_tax) == 1 would be equivalent to parseInt(data.quote_tax) in your case.


However, I would stick with the first approach in this situation:

data.quote_tax == '1'

This requires no type conversions, the intent is immediately obvious and is also doesn't require the developer to know the things I listed above to be understood. Don't make your code more plicated than it has to be.

You can do something like this:

!!parseInt('1'); //true
!!parseInt('0'); //false

Personally I always try and stay away from coercing types. The problem is that if you do as others have suggested, if (x) this works great for 0 & 1. What if for some reason you get a "false", 2, null or undefined. What would you want your code to do here?

undefined ? console.log(true) : console.log(false);
null ? console.log(true) : console.log(false);
NaN ? console.log(true) : console.log(false);
2 ? console.log(true) : console.log(false);
"false" ? console.log(true) : console.log(false);
"true" ? console.log(true) : console.log(false);

If you use === (as opposed to ==) it prevents Javascript coercion. You can then test for the string value "1"

"1" === "1" ? console.log(true) : console.log(false);
1 === "1" ? console.log(true) : console.log(false);
undefined === "1" ? console.log(true) : console.log(false);
null === "1" ? console.log(true) : console.log(false);
NaN === "1" ? console.log(true) : console.log(false);
2 === "1" ? console.log(true) : console.log(false);
"false" === "1" ? console.log(true) : console.log(false);
"true" === "1" ? console.log(true) : console.log(false);

It now works a lot more consistently. May or may not be an issue in your case but I prefer this method for the just in case factor.

本文标签: jqueryString to Boolean using ternary operator in JavascriptStack Overflow