Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

javascript - boolean in an if statement

Today I've gotten a remark about code considering the way I check whether a variable is true or false in a school assignment.

The code which I had written was something like this:

var booleanValue = true;

function someFunction(){
    if(booleanValue === true){
        return "something";
    }
}

They said it was better/neater to write it like this:

var booleanValue = true;

function someFunction(){
    if(booleanValue){
        return "something";
    }
}

The remark which I have gotten about the "=== true" part was that it was not needed and could create confusion.

However my idea is that it is better to check whether the variable is a boolean or not, especially since Javascript is a loosetyped language.

In the second example a string would also return "something";

So my question; Is it neater to loose the "=== true" part in the future, or is it good practise to check the type of the variable as well.

Edit: In my "real" code the boolean represents whether an image has been deleted or not, so the only values boolValue should ever have is true or false.

0 and 1 for example shouldn't be in that variable.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First off, the facts:

if (booleanValue)

Will satisfy the if statement for any truthy value of booleanValue including true, any non-zero number, any non-empty string value, any object or array reference, etc...

On the other hand:

if (booleanValue === true)

This will only satisfy the if condition if booleanValue is exactly equal to true. No other truthy value will satisfy it.

On the other hand if you do this:

if (someVar == true)

Then, what Javascript will do is type coerce true to match the type of someVar and then compare the two variables. There are lots of situations where this is likely not what one would intend. Because of this, in most cases you want to avoid == because there's a fairly long set of rules on how Javascript will type coerce two things to be the same type and unless you understand all those rules and can anticipate everything that the JS interpreter might do when given two different types (which most JS developers cannot), you probably want to avoid == entirely.

As an example of how confusing it can be:

var x;

x = 0;
console.log(x == true);   // false, as expected
console.log(x == false);  // true as expected

x = 1;
console.log(x == true);   // true, as expected
console.log(x == false);  // false as expected

x = 2;
console.log(x == true);   // false, ??
console.log(x == false);  // false 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...