logical-assignment operators

Brendan Eich brendan at mozilla.org
Tue Feb 13 10:19:23 PST 2007


On Feb 13, 2007, at 6:14 AM, liorean wrote:

> On 13/02/07, Lars T Hansen <lth at acm.org> wrote:
>> On Feb 13, 2007, at 3:56 AM, John Cowan wrote:
>> > Brendan Eich scripsit:
>> >>> Yes, but when $x is 0, it remains 0.
>> >> Right -- but why would ES4 differ here?  ES1-3, any old JS in a
>> >> browser, would evaluate 'x = 0; x = x && x + 5; x' to result in 0
>> >> too. I did not follow why your original post made the null initial
>> >> value case (how about undefined?) special compared to 0, false, or
>> >> ''.
>> > Brain fart on my part (comes of knowing too many languages with
>> > imperfect
>> > recall of the details).  Yes, that's flaky in any language where
>> > zero is false, including Javascript.
>
> This is really a problem with using || and && as default and guard
> operators. I've seen plenty code break because of || used as default
> operator when the empty string or zero are meant to be legal values.

Any code in public JS libraries come to mind?  I believe you, but  
I've never seen such bugs, and || for defaulting is popular in  
Mozilla-hosted JS.  (Here's where you promptly hit me with an lxr  
link to Moz code that abuses || ;-).

> They'd serve as much better default and guard operators if they
> treated the empty string and zero as true. NaN, null, undefined on the
> other hand still makes sense to treat as falsy values in default and
> guard operators.

Ship sailed on that one 12 years ago.

> Maybe adding this behaviour as new operators ||| and &&& with |||= and
> &&&= assignment variants would be an idea?

Inspired by === and !==, but please, let's not. The need for === is  
more acute in light of the non-equivalence-relation mess that is ==  
(useful mess? I have very mixed feelings, but just because its  
intransitive I think == is broken in JS -- and I'm to blame for it!  
When we were standardizing ES1, Guy Steele was more jaded; he noted  
that Common Lisp has something like five kinds of "equality"  
operators, so JS had a long way to go ;-)). Here, absent more  
evidence of misuse of || in the field, and without a clearer  
connotation for |||, I would be against adding any such operators.

/be



More information about the Es4-discuss mailing list