最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do the JavaScript relational comparison operators coerce types? - Stack Overflow

programmeradmin0浏览0评论

What rules apply for the JavaScript relational comparison operators when the operands are of different types?

For example, how is true > null evaluated? I can type this into my developer console and it gives the result true, but why?

I searched for a bit, but didn't find any blog posts explaining this, although there are plenty explaining type coercion for == and === comparison operators.

What rules apply for the JavaScript relational comparison operators when the operands are of different types?

For example, how is true > null evaluated? I can type this into my developer console and it gives the result true, but why?

I searched for a bit, but didn't find any blog posts explaining this, although there are plenty explaining type coercion for == and === comparison operators.

Share Improve this question edited Feb 4, 2013 at 13:43 Caspar asked Feb 4, 2013 at 13:33 CasparCaspar 7,7595 gold badges33 silver badges41 bronze badges 3
  • 1 Dear downvoters, please note that StackOverflow explicitly encourages asking and answering your own questions, so I would appreciate some feedback explaining why my question is receiving downvotes while my answer is receiving upvotes :-) – Caspar Commented Feb 4, 2013 at 13:40
  • 3 Your answer is quite good. Your question... not so much. Remember, self-answered questions are indeed encouraged but getting the question part right is hard. If your question is not of outstanding quality and is not marked community wiki, it may well be downvoted or even closed. See meta.stackexchange.com/questions/163623/… for examples of what you can do to curb this. – Frédéric Hamidi Commented Feb 4, 2013 at 13:47
  • I'm not one of the downvoters, but I believe the question is too generic. It's a little better after the edit, and can now be read as a request for an explanation of the null > true case. That's more Stack Overflow-ish. – bfavaretto Commented Feb 4, 2013 at 13:51
Add a comment  | 

1 Answer 1

Reset to default 26

JavaScript relational comparison operator type coercion is defined in the JavaScript specification, specifically in sections 11.8 to 11.8.5 which describe the operators, and sections 9.1 (ToPrimitive) and 9.3 (ToNumber) which describe the process of coercing the operands.

In short, the 4 comparison operators (<, >, <=, and >=) do their best to convert each operand to a number, then compare the numbers. The exception is when both operands are strings, in which case they are compared alphabetically.

Specifically,

  1. If an argument o is an object instead of a primitive, try to convert it to a primitive value by calling o.valueOf() or - if o.valueOf wasn't defined or didn't return a primitive type when called - by calling o.toString()

  2. If both arguments are Strings, compare them according to their lexicographical ordering. For example, this means "a" < "b" and "a" < "aa" both return true.

  3. Otherwise, convert each primitive to a number, which means:

  • undefined -> NaN
  • Null -> +0
  • Boolean primitive type -> 1 if true, +0 if false
  • String -> try to parse a number from the string
  1. Then compare each item as you'd expect for the operator, with the caveat that any comparison involving NaN evaluates to false.

So, this means the following:

console.log(true > null);           //prints true
console.log(true > false);          //prints true
console.log("1000.0" > 999);        //prints true
console.log("  1000\t\n" < 1001);   //prints true

var oVal1 = { valueOf: function() { return 1; } };
var oVal0 = { toString: function() { return "0"; } };

console.log(oVal1 > null);         //prints true
console.log(oVal0 < true);         //prints true
console.log(oVal0 < oVal1);        //prints true
发布评论

评论列表(0)

  1. 暂无评论