I assume you understand the first two boolean, and have some confusion in the last one.
Your question is, if first 2 are true
, why the last one is not true
.
There is a concept of thruthy
and falsey
values in Javascript. A thruthy
value may or may not be a Boolean
, but still satisfies the if
conditions.
For example, in case of string, an empty string is a falsey
value, and a string with even a single character is thruthy
.
A truthy
value is equivalent to true
, but not equals to true
. Same for falsey
.
In the last case, ("" || "word") == true
, the left part equals word
, and the operator used, ==
checks if the left value equals right value. So word
is not equal to true
, hence returns false
.
("" || "word") == true // lets simplify
"word" == true // false, "word" is not equal to true
But if you use word
in an if
condition, it will satisfy the condition since it's a thruthy
value.
if ("word") { /* condition will be satisfied */ }
if ("") { /* condition will NOT be satisfied */ }
To check whether a value is truthy
or not, use !!
before the value.
// String
!!"word" // true
!!"" // false
// Number
!!5 // true
!!0 // false
// Boolean
!!true // true
!!false // false