From 00415c3692a1f1f54ff4bce02fce56548be10237 Mon Sep 17 00:00:00 2001 From: Aleksandras Date: 2023年8月14日 02:05:00 +0300 Subject: [PATCH] Clarification --- 1-js/02-first-steps/08-operators/article.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/1-js/02-first-steps/08-operators/article.md b/1-js/02-first-steps/08-operators/article.md index d52c37a172..199355cdb2 100644 --- a/1-js/02-first-steps/08-operators/article.md +++ b/1-js/02-first-steps/08-operators/article.md @@ -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". + +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`. +```` + 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.

AltStyle によって変換されたページ (->オリジナル) /