Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript if(x) vs if(x==true)

Tags:

javascript

In JavaScript , in which cases the following statements won't be logically equal ?

if(x){}

and

if(x==true){}

Thanks

like image 855
TheZver Avatar asked Apr 14 '14 13:04

TheZver


People also ask

Why == is false in JavaScript?

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.

How do you check if a boolean is true in JS?

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) .

How do you check if something is true in JavaScript?

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!

What does X true mean?

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.


Video Answer


1 Answers

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
like image 101
thefourtheye Avatar answered Sep 18 '22 05:09

thefourtheye