Javascript TRUE and "true", why does someone use a string instead of boolean?

In some javascript codes, I see people write something like this:

var myVar = "true";

//...

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

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

Or just bad code ... and try to never write that way.

+5
source share
3 answers

This is just bad code. Try to never write this way.

This type of code is just awful for maintainability. And ==(instead ===) and trueas a string.

PS: besides that "true" == true // false. For the argument, ===this is simply because true == 1 // truemany others are similar to such things.

+5
source

, , , true - :). (===) .

, , .

+4

This is just bad code, as you say.

A "real" developer never writes if (condition == true), but onlyif (condition)

You can also record if (true == condition). This is called the Yoda style and is designed to prevent unwanted variable assignments if you mistakenly write =instead ==.

+1
source

All Articles