Reputation: 41
console.log([1,2,3] > null); // returns false
// "1,2,3" > null
// NaN > null
// false => Direct returns false ??
The code returns false. Normally NaN > null must be numeric comparison. But result returned false. Why?
Upvotes: 1
Views: 60
Reputation: 1075567
Following the steps in the Abstract Relational Comparison algorithm:
[1,2,3] > null
- Step 1 (a,b): Apply ToPrimitive to both sides"1,2,3" > null
- Step 4 (d, e): Apply ToNumeric to both sidesNow we have NaN > 0
, which is false
, because any time NaN
is involved in any relational operation, the result is false
.
In a comment on the question you asked:
But why returns false without comparison.
>
is a comparison. I think you mean "without a branching operation or expression" (like if
or the conditional operator, ? :
). If so, it's because expressions (including relational expressions) have result values whether or not you use those results for branching. The result of a >
expression is true
or false
. If you use that in an if
, that's fine, but you don't have to:
const a = 1 > 2;
console.log(a); // false
Upvotes: 3