Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
sealep opened this issue Oct 6, 2023 · 9 comments · Fixed by #29564
Closed

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

sealep opened this issue Oct 6, 2023 · 9 comments · Fixed by #29564
Labels
Content:JS JavaScript docs good first issue A good issue for newcomers to get started with.

Comments

@sealep
Copy link

sealep commented Oct 6, 2023

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
@sealep sealep added the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label Oct 6, 2023
@github-actions github-actions bot added the Content:JS JavaScript docs label Oct 6, 2023
@Josh-Cena Josh-Cena added good first issue A good issue for newcomers to get started with. and removed needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. labels Oct 6, 2023
@skyclouds2001
Copy link
Contributor

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
Copy link
Contributor

@Josh-Cena
Copy link
Member

Josh-Cena commented Oct 7, 2023

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
Copy link
Contributor

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
Copy link
Member

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
Copy link
Contributor

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

@Josh-Cena
Copy link
Member

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

@skyclouds2001
Copy link
Contributor

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
Copy link
Member

@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.

Josh-Cena added a commit that referenced this issue Oct 13, 2023
* equivalence of x &&= y and x && ( x = y ) #29524

* Equivalence of operators

* Delete .vscode/settings.json

* Apply suggestions from code review

---------

Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:JS JavaScript docs good first issue A good issue for newcomers to get started with.
Projects
None yet
6 participants
@Josh-Cena @mighty-odewumi @manviii27 @skyclouds2001 @sealep and others