Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
Alexandre887 wants to merge1 commit intojavascript-tutorial:master
base:master
Choose a base branch
Loading
fromAlexandre887:master-11
Open
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions1-js/02-first-steps/08-operators/article.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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**):
Copy link
Contributor

@shallow-beachshallow-beachAug 27, 2024
edited
Loading

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 runlet a;a = 1 + 2, 3 + 4;alert(a); // 3```

Copy link
Contributor

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


```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”.
Copy link
Contributor

@shallow-beachshallow-beachAug 27, 2024
edited
Loading

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


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

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 + 4 evaluates+ 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 of3+4?

````

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.
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp