I am trying to use some Booleans
When doing as I do with other languages: eg Java
if(varName){}
But when doing it in Javascript it doesn't work as I want
Is it always necessary to do if(varName == true)?
Or is there a way to just do if(varName) ?
var b1 = Boolean('true');
var b2 = Boolean('false');
if (b1 && b2) {
console.log("entro al if")
}
if (b1) {
console.log("entro al if")
}
if (b2) {
console.log("entro al if")
}
if (b2 == false) {
console.log("entro al if")
}
As in
java
, the statement forclase
, is different from thedeclaracion primitiva
.Being
Boolean
andboolean
different objects, the first is generic, the second is primitive, if you compare bothjava
without usingequals
the result should always befalse
indistinctly.The same thing happens in
javascript
, where creating a boolean from the class is not the same as creating abooleano primitivo
:An easy way to fix it is to simply not use the generic object but the primitive instead, going from having the code above to:
Which YES , gives us the expected result in this case, there is also another way to obtain the expected result, and that is by creating your
boolean
from aboolean
, instead of using a string:So we see an obvious difference between using a
string
to create aBoolean
, and aboolean
to create aboolean
.About whether it is necessary to use the condition:
The answer is definitely no , if it
varName
is aboolean
, then this would be exactly the same and it would work:But why using the constructor
Boolean
gives different results then?The answer is because the constructor
Boolean
accepts primitive values in a range of0
a1
, as can be logical... in whichfalse
is treated as0
andtrue
is treated as1
, so the constructorBoolean
can actually receive 2 values, either an integer between0
and1
or directly aboolean
which translates either to0
or1
(false
ortrue
), whenBoolean
you pass something other than a number between0
and1
or directlyBoolean
to the constructor, the constructor proceeds to do a checkbooleana
such as:Where whatever you pass to it, if it satisfies the condition the value is determined as
true
, while if it doesn't satisfy the condition the value is determined asfalse
.The thing is that the text strings when passed in the constructor are considered as
true
, we can check it here:This is exactly the problem, what is happening to you when creating your
Boolean
is that actually both values aretrue
without you realizing it when using astring
as value to create aboolean
:Since both are
true
and you expect one of them to befalse
, that's why the result, let's see what happens if we have the same condition with numbers:As we can see, they are correctly evaluated as
true
orfalse
.That's why this strange behavior occurs.
Any non-empty string that you try to translate to a Boolean value (either via
Boolean(string)
or !!string) will return the Boolean valuetrue
, this is because any non-empty string is a valuetruthy
regardless of its content.A little snippet with an example:
This is the same as saying that the Boolean value of a
string
will be false as long as it is empty.The option that I see, if, as you commented in a previous answer, the value you receive is a string with 'true' or 'false', is to evaluate that in a function and compare based on that.
A small example: