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.
This question is related to
javascript
semantics
This depends. If you are concerned that your variable could end up as something that resolves to TRUE. Then hard checking is a must. Otherwise it is up to you. However, I doubt that the syntax whatever == TRUE
would ever confuse anyone who knew what they were doing.
Revisa https://www.w3schools.com/js/js_comparisons.asp
example:
var p=5;
p==5 ? true
p=="5" ? true
p==="5" ? false
=== means same type also same value == just same value
The identity (===)
operator behaves identically to the equality (==)
operator except no type conversion is done, and the types must be the same to be considered equal.
It depends on your usecase. It may make sense to check the type too, but if it's just a flag, it does not.
If you write: if(x === true)
, It will be true for only x = true
If you write: if(x)
, it will be true for any x that is not: '' (empty string), false, null, undefined, 0, NaN.
Since the checked value is Boolean
it's preferred to use it directly for less coding and at all it did same ==true
In Javascript the idea of boolean is fairly ambiguous. Consider this:
var bool = 0
if(bool){..} //evaluates to false
if(//uninitialized var) //evaluates to false
So when you're using an if statement, (or any other control statement), one does not have to use a "boolean" type var. Therefore, in my opinion, the "=== true" part of your statement is unnecessary if you know it is a boolean, but absolutely necessary if your value is an ambiguous "truthy" var. More on booleans in javscript can be found here.
If the variable can only ever take on boolean values, then it's reasonable to use the shorter syntax.
If it can potentially be assigned other types, and you need to distinguish true
from 1
or "foo"
, then you must use === true
.
Also can be tested with Boolean object, if you need to test an object
error={Boolean(errors.email)}
I think that your reasoning is sound. But in practice I have found that it is far more common to omit the ===
comparison. I think that there are three reasons for that:
undefined
or null
value. Often you just want your test to fail in such cases. (Though I try to balance this view with the "fail fast" motto).Consider this example:
var someString = getInput();
var normalized = someString && trim(someString);
// trim() removes leading and trailing whitespace
if (normalized) {
submitInput(normalized);
}
I think that this kind of code is not uncommon. It handles cases where getInput()
returns undefined
, null
, or an empty string. Due to the two boolean evaluations submitInput()
is only called if the given input is a string that contains non-whitespace characters.
In JavaScript &&
returns its first argument if it is falsy or its second argument if the first argument is truthy; so normalized
will be undefined
if someString
was undefined and so forth. That means that none of the inputs to the boolean expressions above are actually boolean values.
I know that a lot of programmers who are accustomed to strong type-checking cringe when seeing code like this. But note applying strong typing would likely require explicit checks for null
or undefined
values, which would clutter up the code. In JavaScript that is not needed.
In the plain "if" the variable will be coerced to a Boolean and it uses toBoolean on the object:-
Argument Type Result
Undefined false
Null false
Boolean The result equals the input argument (no conversion).
Number The result is false if the argument is +0, -0, or NaN;
otherwise the result is true.
String The result is false if the argument is the empty
String (its length is zero); otherwise the result is true.
Object true.
But comparison with === does not have any type coercion, so they must be equal without coercion.
If you are saying that the object may not even be a Boolean then you may have to consider more than just true/false.
if(x===true){
...
} else if(x===false){
....
} else {
....
}
Since you already initialized clearly as bool, I think ===
operator is not required.
In general, it is cleaner and simpler to omit the === true
.
However, in Javascript, those statements are different.
if (booleanValue)
will execute if booleanValue
is truthy – anything other than 0
, false
, ''
, NaN
, null
, and undefined
.
if (booleanValue === true)
will only execute if booleanValue
is precisely equal to true
.
Source: Stackoverflow.com