In JavaScript , in which cases the following statements won't be logically equal ?
if(x){}
and
if(x==true){}
Thanks
Javascript is like Java in that the == operator compares the values of primitive types, but the references of objects. You're creating two arrays, and the == operator is telling you that they do not point to the same exact object in memory: var b = new Array( 1, 2, 3 ); var c = new Array( 1, 2, 3 ); console.
An alternative approach is to use the logical OR (||) operator. To check if a value is of boolean type, check if the value is equal to false or equal to true , e.g. if (variable === true || variable === false) .
Use the strict equality (===) operator to check if a variable is equal to true - myVar === true . The strict equality operator will return true if the variable is equal to true , otherwise it will return false . Copied!
if you use if x ,it means it has to evaluate x for its truth value. But when you use x ==True or x is True . It means checking whether type(x)==bool and whether x is True. attention : x is True is no equal to bool(x)==True.
They are not at all equal.
if (x)
checks if x
is Truthy where as the later checks if the Boolean value of x
is true
.
For example,
var x = {};
if (x) {
console.log("Truthy");
}
if (x == true) {
console.log("Equal to true");
}
Not only an object, any string (except an empty string), any number (except 0
(because 0
is Falsy) and 1
) will be considered as Truthy, but they will not be equal to true.
As per ECMA 5.1 Standards, in if (x)
, Truthiness of x
will be decided, as per the following table
+-----------------------------------------------------------------------+
| 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 |
+-----------------------------------------------------------------------+
Note: The last line object
, which includes both objects and Arrays.
But in the later case, as per The Abstract Equality Comparison Algorithm,
If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
value of x
will be converted to a number and that number will be checked against true
.
Note:
In JavaScript, true
is 1
and false
is 0
.
console.log(1 == true);
# true
console.log(0 == false);
# true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With