Operators ||= and &&=

Peter Michaux petermichaux at gmail.com
Tue May 5 11:37:43 PDT 2009

On Mon, May 4, 2009 at 10:26 PM, Brendan Eich <brendan at mozilla.com> wrote:

> ??= and ?? are too narrowly typed for JS.

I had the impression from the previous thread that you were more in
favor of ??= than ||=. People often write the following at the
beginning of functions for optional parameters

  optionalParam = optionalParam || defaultVal;

I believe what they are really trying to write is

  optionalParam = optionalParam === undefined ? defaultValue : optionalParam;

which is what ??= would do.

I think optional parameters are the nuisance use case that is fueling
any discussion about a ||= or ??= operator. I think other use cases
are less common and probably would not warrant adding a new operator.
The reason I mention this is if there is another sugar plan for
default parameter values then perhaps ||= and ??= aren't needed at all
(though I think they are probably worth adding.)

I thought default parameters were going to be something like the following.

  function(a=1, b="defaultVal");

And in this syntax will default values be used if the parameter is
falsey or only if it is undefined?


More information about the es-discuss mailing list