Why does 0 < undefined
return false
instead of undefined?
Based on the abstract relational comparison part of the ES6 spec http://www.ecma-international.org/ecma-262/6.0/#sec-abstract-relational-comparison I think it should return undefined. (please click the spec to understand what I'm saying)
I don't entirely understand the ReturnIfAbrupt part, but it doesn't seem like that should stop the comparison. Then I interpreted 3 and 4 as basically saying since its using a < flag and not a > flag then keep on going. It should ignore 5 because 0 and undefined are both not strings.
So then on 6. it says to convert 0 to a number, which is 0 and to convert undefined to a number which should be NaN as per http://www.ecma-international.org/ecma-262/6.0/#sec-tonumber
Since 6f states "If ny is NaN, return undefined" then why am I getting false rather than undefined? It's the same result in Chrome, Safari and Firefox so I'm assuming all 3 browsers aren't interpreting the spec wrong.
The answer is that you are looking at the specification of the Abstract Relational Comparison operation but not the <
operator.
The specification of the <
operator falls under Relational Operators in ES6: http://www.ecma-international.org/ecma-262/6.0/#sec-relational-operators-runtime-semantics-evaluation
Paraphrasing the spec, the <
operator works as follows:
So there is a hardcoded part of the spec that converts the undefined
return value from the Abstract Relational Comparison operation to false
Note: It's specified the same way in ES7: http://www.ecma-international.org/ecma-262/7.0/#sec-relational-operators-runtime-semantics-evaluation so it's not going to change. Also, it was specified that way in ES5 as well.