mdn / content

The content behind MDN Web Docs
https://developer.mozilla.org
Other
9.16k stars 22.46k forks source link

equivalence of x &&= y and x && ( x = y ) #29524

Closed sealep closed 1 year ago

sealep commented 1 year ago

MDN URL

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND_assignment

What specific section or headline is this issue about?

Description

What information was incorrect, unhelpful, or incomplete?

Worth mentioning that x &&= y is equivalent to x && ( x = y ) except that x is only evaluated once.

What did you expect to see?

Elaboration that x is evaluated only once in x &&= y.

Do you have any supporting links, references, or citations?

Caveat that in x &&= y, x is evaluated only once, contrary to x && ( x = y ).

Do you have anything more you want to share?

No response

MDN metadata

Page report details * Folder: `en-us/web/javascript/reference/operators/logical_and_assignment` * MDN URL: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND_assignment * GitHub URL: https://github.com/mdn/content/blob/main/files/en-us/web/javascript/reference/operators/logical_and_assignment/index.md * Last commit: https://github.com/mdn/content/commit/f616cb604af851f77f8cd59368e94ee3e43a8838 * Document last modified: 2023-08-15T14:59:44.000Z
skyclouds2001 commented 1 year ago

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR_assignment

Logical OR assignment short-circuits, meaning that `x ||= y` is equivalent to `x || (x = y)`;

has the same issue

skyclouds2001 commented 1 year ago

Same in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_assignment

Josh-Cena commented 1 year ago

Yep, these three pages share the same structure, and I have a script to check that. So whoever updates one of them will be reminded by me that the rest should be updated (or if the PR is merged by someone else ahead of me, I can send a followup)

Actually I think this should be mentioned on every single compound assignment page: x += y is equivalent to x = x + y except x is only evaluated once, etc.

mighty-odewumi commented 1 year ago

Hi @Josh-Cena, I would like to work on this issue.

I believe the fix is that the documentation should specify that x or whichever variable comes first is only run once, right?

Josh-Cena commented 1 year ago

On all compound assignment operator pages, there is a sentence like:

x += y is equivalent to x = x + y.

meaning that x &&= y is equivalent to:

x && (x = y);

Your job is to make it read:

x += y is equivalent to x = x + y, except that the expression x is only evaluated once.

x && (x = y);

Except that the expression x is only evaluated once.

manviii27 commented 1 year ago

Hi @Josh-Cena, can I work on this issue??

Josh-Cena commented 1 year ago

No need to ask. If anyone wants to send a PR, please do it right away.

skyclouds2001 commented 1 year ago

Personally, I think it is better to use x = x ?? y format rather than x ?? ( x = y ) format, as it is better for reader to understand

Josh-Cena commented 1 year ago

@skyclouds2001 they mean different things. The paragraph below are exactly to illustrate why x = x ?? y is NOT equivalent, because it performs assignment even when x is not nullish.