If the equality (strict or not) operator returns true for undefined...
console.log(undefined == undefined)
...why does the greater or equal than operator returns false?
console.log(undefined >= undefined)
The MDN page says that the operands are compared using the Abstract Relational Comparison, but that refers to greater than (or less than) part. Thus, it seems to me that the greater than or equal should provide the same algorithm for equality, isn't that correct?
Thus, it seems to me that the greater than or equal should provide the same algorithm for equality, isn't that correct?
No, that algorithm is not involved here. The spec defines the evaluation of >= as
- Let lref be the result of evaluating RelationalExpression.
- Let lval be ? GetValue(lref).
- Let rref be the result of evaluating ShiftExpression.
- Let rval be ? GetValue(rref).
- Let r be ? IsLessThan(lval, rval,
true).- If r is
trueorundefined, returnfalse. Otherwise, returntrue.
So the only algorithm involved is IsLessThan, which will convert its operands to numbers if they are not already numbers (or strings).
undefined gets converted to NaN and Number::lessThan returns undefined if any operand is NaN, which, according to step 6 above results in false.