admin管理员组文章数量:1333402
Why does (1 < NaN)
give back false
and not undefined
(in JavaScript)?
In "11.8.5 The Abstract Relational Comparison Algorithm" it says that if either of the values is NaN (after ToPrimitive and ToNumber which should not affect NaN in my view) the result is undefined
.
In FF and Chrome I get:
console.log(1 < NaN);
// false
Why is that?
Why does (1 < NaN)
give back false
and not undefined
(in JavaScript)?
In "11.8.5 The Abstract Relational Comparison Algorithm" it says that if either of the values is NaN (after ToPrimitive and ToNumber which should not affect NaN in my view) the result is undefined
.
In FF and Chrome I get:
console.log(1 < NaN);
// false
Why is that?
Share Improve this question edited Jul 14, 2011 at 18:53 sth 230k56 gold badges287 silver badges370 bronze badges asked Jul 13, 2011 at 9:41 SachaSacha 2011 silver badge3 bronze badges 3- 5 +1 for quoting the spec in a question. :-) – RobG Commented Jul 13, 2011 at 9:47
- 3 You should definitely look into false/NaN/0/undefined/""/null problems of Javascript. It's horribly inconsistent and one of most serious flaws of this (otherwise quite nice) language. – SF. Commented Jul 13, 2011 at 9:48
- @SF: And if it had been really strict, people would plain about that instead. :-) The rules aren't really all that bad, and in fact this particular example has nothing to do with the false/NaN/0/undefined/""/null thing. It's more that the specification has this section saying how relations work, but you then have to look elsewhere to see that having done the work in the quoted section, there's more that the actual operator does. – T.J. Crowder Commented Jul 13, 2011 at 9:55
1 Answer
Reset to default 15Because the <
operator returns false
when the abstract relational algorithm returns undefined
. See Section 11.8.1:
11.8.1 The Less-than Operator (
<
)The production RelationalExpression : RelationalExpression < ShiftExpression is evaluated as follows:
- 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 the result of performing abstract relational parison lval < rval. (see 11.8.5)
- If r is undefined, return false. Otherwise, return r.
This is true of all of the relational operators. The algorithm has an undefined
oute, but the operators convert that to false
. And that makes sense. 1
isn't < NaN
(nor is it > NaN
, or == NaN
, or... :-) ).
(Nice to see people reading the spec.)
本文标签: Why is (1 lt NaN) false in JavaScriptStack Overflow
版权声明:本文标题:Why is (1 < NaN) false in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742242385a2438914.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论