-
Notifications
You must be signed in to change notification settings - Fork 4k
Clarification in the example of comma operator #3542
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
Conversation
@shallow-beach
shallow-beach
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for adding an executable to this tricky caveat!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
simplify to use strict:
Try running the following code:
```js run
let a;
a = 1 + 2, 3 + 4;
alert(a); // 3
```
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not totally sure why doesn't work with inline declaration tbh. let modifies operation order somehow i guess? could be part of explanation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a little more direct:
This is confusing because the
,operator should "evaluate each expression, but return the result of only the last one".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe
Without parentheses,
a = 1 + 2, 3 + 4evaluates+first, summing the numbers intoa = 3, 7, then the assignment operator=assignsa = 3, and the rest (, 7) is ignored. It's like(a = 1 + 2), 3 + 4.
to emphasize the evaluation of 3+4?
javascript-translate-bot
commented
Aug 27, 2024
Please make the requested changes. After it, add a comment "/done".
Then I'll ask for a new review 👻
Basic operators, maths
I think we need to add a clarification to this example, because if we run
a = 1 + 2, 3 + 4or(a = 1 + 2), 3 + 4in strict mode, we getReferenceError: a is not defined. I've seen several comments regarding this (some even tried to run this line of code withlet:let a = 1 + 2, 3 + 4)