Tony
Tony

Reputation: 12695

JavaScript parsing boolean

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

Upvotes: 3

Views: 7295

Answers (2)

Jonas G. Drange
Jonas G. Drange

Reputation: 8845

This is ugly, but why not?

if (file.showInTable === "false") file.showInTable = false;

Upvotes: 1

jantimon
jantimon

Reputation: 38142

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) {

} 

Upvotes: 2

Related Questions