Operators ||= and &&=

Brendan Eich brendan at mozilla.com
Wed May 6 14:38:26 PDT 2009


On May 6, 2009, at 1:43 PM, Ash Berlin wrote:

>> I don't think ||= and ??= are very difficult to define clearly.
>> Perhaps just a line each in terms of the expanded syntax. I don't
>> think they would add much bloat to engines. Perhaps just better to  
>> add
>> them both and move on to discussing classes, lambdas, or processes.
>>
>
> a ||= b;
> a = a || b;

Is the reference to |a| computed only once, and before b is evaluated?  
This matters in JS since evaluating b could change where |a| exists on  
the scope chain.


> a ??= b;
> a = a === undefined ? b : a;

In no case should |a| be evaluated twice here, but this desugaring  
does that.

Nothing's ever as simple as you want :-P.


> You could make a case for adding a ?? operator for symmetry. Example  
> of usage:
>
> some_func(a, b ?? "c" );

Roger that, given ??= it would be strange to leave out ??.

I'm still trying to leave out both ;-).

/be


More information about the es-discuss mailing list