Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript TRUE and "true" why someone uses string instead of boolean?

I see in some javascript codes that people write something like this:

var myVar = "true";

//...

if(myVar == "true") {
     //...
}else{
     //...
}

Why people don't use TRUE or FALSE? As far as I know boolean type is obvious for browsers.

Or is just a poor code ... and try to never write in this way.

like image 449
Radek Avatar asked Jul 10 '12 12:07

Radek


2 Answers

It's just poor code. Try to never write in this way.

This kind of code is just horrible for maintainability. Both the == (instead of ===) and the true as string.

PS: besides, "true" == true // false. For the === argument, it's simply because true == 1 // true, and a lot of others look alike stuff like this.

like image 59
Florian Margaine Avatar answered Oct 01 '22 12:10

Florian Margaine


You should not do this, unless you really expect a string that contains true for some reason :). But even in that case, using strict equality (===) would be the right choice.

In the code example you are showing, this is simply a terrible way of writing code.

like image 26
kapa Avatar answered Oct 01 '22 12:10

kapa