logical-assignment operators

liorean liorean at gmail.com
Tue Feb 13 06:14:19 PST 2007


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.
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.

Maybe adding this behaviour as new operators ||| and &&& with |||= and
&&&= assignment variants would be an idea?
-- 
David "liorean" Andersson



More information about the Es4-discuss mailing list