According to ECMA-262, part 11.13, following is the exhaustive list of compound assignment operators: *= /= %= += -= <<= >>= >>>= &= ^= |=
.
According to the part 11.11, var c = a || b
will put a
value into c
if ToBoolean(a)
is true and will put b
value into c
otherwise. As such, the logical OR is often used as the coalesce operator, e.g.
function (options) {
options = options || {};
}
Frequently enough, coalesce is used to specify the default value for the variable, as was shown above: a = a || b
.
It seems that compound assignment operator ||=
would be really useful, allowing to write the code above in a shorter and cleaner fashion: a ||= b
. However, it is not there (although *=
, +=
and other compound assignment operators are).
The question is, why?
+=
,*=
,-=
,/=
, why wouldn't%=
work?).angle %= 360
orvertexIndex %= numberOfVertices
(for the vertex list of a closed polygon).||=
and&&=
would be coming soon: "You may be glad to learn that for JS2/ES4, ||= and &&= are being added as well. ||= is most useful of the two but there’s no reason to leave the assignment-op form of && out.".