DEV Community

loading...

Discussion on: ES 2021 Features

Collapse
dylanoshima profile image
Dylan

From the proposal I believe a ||= b is syntactic sugar for:
a = a || b, but is meant to not trigger the object's setter function (if it has one).

They use the example: obj.a = obj.a ?? b would trigger the setter for obj.a since it's being reassigned in both instances. To prevent this we could do obj.a ?? (obj.a = b); which would only trigger the reassignment if obj.a == null, but apparently that's weird. Thus to make it succinct they propose:
a ??= b.
Which will not trigger the setter unless it needs to.

Collapse
tyetyetye profile image
tyetyetye

Thanks!