If the empty array
console.log( == false); // prints true console.log( ? 0 : 1); // prints 0 !
First, an empty array is not falsey, to get the actual boolean value prefix it with
!! such as:
console.log(!!) // true
The result you got from the first comparison is due to coercion. When doing a left vs. right comparison via the
Booleanso it is converted to a
Numbervalue, resulting in the comparison being
 == 0.
Objectit is converted to a primitive; in this case a
.toString(), resulting in the comparison being
"" == 0.
String, it gets converted to a
numbervalue resulting in
0 == 0.
Numberprimitives, their values are compared, resulting in a truthy condition
With the second expression, there is no right-hand to compare to, so it simply tests if the input is truthy. The array is not
NaN, therefore, it is truthy.
To force a non-coercive comparison between two inputs you should use the
adding on the above answers..
to get the true boolean value of any value you can prefix it with
!!(in this way you won't run in any coercion issues);
in your case you can do this:
console.log(!! == false) //logs false
Just to remember.
===: Checks the type as well as the value
== Checks only the value.
Try these and check the results:
console.log( === false); //prints false
console.log( == false); //prints true
console.log(0 == false); //prints true
console.log(1 == "1") // prints true
console.log(1 === "1") //prints false
I hope It helps.
©2020 All rights reserved.