Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript parsing boolean

Tags:

javascript

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

like image 611
Tony Avatar asked Mar 07 '26 04:03

Tony


2 Answers

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

} 
like image 99
jantimon Avatar answered Mar 09 '26 23:03

jantimon


This is ugly, but why not?

if (file.showInTable === "false") file.showInTable = false;
like image 40
Jonas G. Drange Avatar answered Mar 09 '26 21:03

Jonas G. Drange



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!