Logical operators don't use valueOf()

Allen Wirfs-Brock allen at wirfs-brock.com
Sat Sep 7 11:08:26 PDT 2013


On Sep 7, 2013, at 9:10 AM, Marius Gundersen wrote:

> While playing around with valueOf, I discovered that three operators (!, ||, &&) don' seem to use valueOf when you use them. You can test this in your preferred browser here: http://lab.mariusgundersen.net/valueOf
> 
> Is there a reason for this? Shouldn't these operators work the same way as the other operators?

That's the way ToBoolean has always been defined http://people.mozilla.org/~jorendorff/es6-draft.html#sec-7.1.2 . All objects are considered to be Boolean true values.

&, | ,^, ~ are actually bitwise operations on numeric values so they do ToInt32 (which may ultimately call valueOf).

!, &&, || are logical boolean operations so they call ToBoolean.

This generally makes sense, and far too much existing code depends upon this behavior to every consider changing it.

Allen


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130907/f4ea0fc5/attachment.html>


More information about the es-discuss mailing list