3

in the response from my server I get a JSON object. It has a boolean flag.

if(file.showInTable == 'true') {

} 

But, even if showInTable is set to false, I get inside that code block. How to cope with that ?

I tried:

if(file.showInTable == 'true')
if(file.showInTable)
if(Boolean(file.showInTable))

Edit

as Ghommey has mentioned, I've used the 2nd option to check that value. Even if the comparions statement returns false, it also gets inside the code. See the pic below

enter image description here

Tony
  • 12,405
  • 36
  • 126
  • 226

2 Answers2

2

it is set to false or true (as bool) - Tony

Why do you compare a boolean as a string?

Just compare it as a boolean:

if(file.showInTable === true) {

} 

or

if(file.showInTable !== false) {

} 
jantimon
  • 36,840
  • 23
  • 122
  • 185
  • `file.showInTable === true` and `file.showInTable !== false` aren't the same comparison. `'true' === true` is false, whereas `'true' !== false` is true. – Anthony Grist Aug 17 '12 at 09:05
  • but Anthony, the thing here is, he receives a boolean object, so file.showInTable will be either `true` or `false` not `'true'` or `'false'`, OP was attempting to equal the boolean he was receiving to a string. – Gonçalo Vieira Aug 17 '12 at 09:14
  • Was it not the complete opposite? – Jonas G. Drange Aug 17 '12 at 09:15
  • a strange thing. I've used the 2nd option to check it's value. When it's `true`, it gets inside, but if that comparsion statement returns `false`, it also gets inside – Tony Aug 17 '12 at 10:23
1

This is ugly, but why not?

if (file.showInTable === "false") file.showInTable = false;
Jonas G. Drange
  • 8,749
  • 2
  • 27
  • 38