3

I have a result coming from PHP side and json_encode send all the values as string (not as it's original type). The string coming 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 coming 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 comparison but this case comparing 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:

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

Liam
  • 27,717
  • 28
  • 128
  • 190
ReynierPM
  • 17,594
  • 53
  • 193
  • 363
  • `"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 Dec 09 '16 at 15:53

5 Answers5

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.

Community
  • 1
  • 1
Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
2

You can combine 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');
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
2

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 complicated than it has to be.

TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94
1

You can do something like this:

!!parseInt('1'); //true
!!parseInt('0'); //false
Piotr Białek
  • 2,569
  • 1
  • 17
  • 26
0

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.

Community
  • 1
  • 1
Liam
  • 27,717
  • 28
  • 128
  • 190