-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -458,12 +458,22 @@ alert( a ); // 7 (the result of 3 + 4) | |
|
||
Here, the first expression `1 + 2` is evaluated and its result is thrown away. Then, `3 + 4` is evaluated and returned as the result. | ||
|
||
```smart header="Comma has a very low precedence" | ||
````smart header="Comma has a very low precedence" | ||
Please note that the comma operator has very low precedence, lower than `=`, so parentheses are important in the example above. | ||
|
||
Without them: `a = 1 + 2, 3 + 4` evaluates `+` first, summing the numbers into `a = 3, 7`, then the assignment operator `=` assigns `a = 3`, and the rest is ignored. It's like `(a = 1 + 2), 3 + 4`. | ||
Try running the following code (**we don't use `"use strict"` in the example below, otherwise we would get an error**): | ||
|
||
```js run no-strict | ||
a = 1 + 2, 3 + 4; | ||
|
||
alert(a); // 3 | ||
``` | ||
|
||
An unusual result, isn't it? Especially considering that 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 commentThe reason will be displayed to describe this comment to others. Learn more. a little more direct:
|
||
|
||
Without them: `a = 1 + 2, 3 + 4` evaluates `+` first, summing the numbers into `a = 3, 7`, then the assignment operator `=` assigns `a = 3`, and the rest is ignored. It's like `(a = 1 + 2), 3 + 4`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe
to emphasize the evaluation of |
||
```` | ||
|
||
Why do we need an operator that throws away everything except the last expression? | ||
|
||
Sometimes, people use it in more complex constructs to put several actions in one line. | ||
|
Uh oh!
There was an error while loading. Please reload this page.
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:
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