最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

What is the point of wrapping JavaScript statements in parentheses? - Stack Overflow

programmeradmin3浏览0评论

I have discovered that wrapping different statements in parentheses will return the last one:

(34892,47691876297,2000)                => 2000
('test',73,document.createElement('p')) => <p></p>

And I also found out that all the statements are executed anyway:

(console.log('test'), console.log('test2'), console.log('test3'), 6)

Will log:

test
test2
test3

And the result will be 6.

However, I've also found that some statements can't be used:

(throw new Error(), 10)         => SyntaxError: Unexpected token throw
(if (1) console.log('test'), 5) => SyntaxError: Unexpected token if

So, what is the point of this parenthesis-ma notation? You could easily execute all the statements and then use the last statement's value. What is this for? Am I using it incorrectly?

I have discovered that wrapping different statements in parentheses will return the last one:

(34892,47691876297,2000)                => 2000
('test',73,document.createElement('p')) => <p></p>

And I also found out that all the statements are executed anyway:

(console.log('test'), console.log('test2'), console.log('test3'), 6)

Will log:

test
test2
test3

And the result will be 6.

However, I've also found that some statements can't be used:

(throw new Error(), 10)         => SyntaxError: Unexpected token throw
(if (1) console.log('test'), 5) => SyntaxError: Unexpected token if

So, what is the point of this parenthesis-ma notation? You could easily execute all the statements and then use the last statement's value. What is this for? Am I using it incorrectly?

Share Improve this question edited Jun 9, 2022 at 20:45 EJoshuaS - Stand with Ukraine 12.2k61 gold badges57 silver badges85 bronze badges asked Apr 29, 2013 at 15:49 tckmntckmn 59.4k27 gold badges118 silver badges156 bronze badges 6
  • A number of languages work this way; Scala works this way. – Robert Harvey Commented Apr 29, 2013 at 15:51
  • 1 @RobertHarvey Well, what is the point of doing this though? I don't understand why you can't just execute all of the statements. – tckmn Commented Apr 29, 2013 at 15:52
  • The throw seems obvious. The second example suggests that the list only accepts basic expressions, not plex code constructs. – Robert Harvey Commented Apr 29, 2013 at 15:55
  • 2 developer.mozilla/en-US/docs/JavaScript/Reference/Operators/… – Ian Commented Apr 29, 2013 at 15:57
  • 3 34892 and console.log('test') are expressions, if and throw are statements. You can apply operators (including the ma operator) to expressions, but not to statements. – georg Commented Apr 29, 2013 at 16:10
 |  Show 1 more ment

2 Answers 2

Reset to default 11

That is the ma operator :)

It lets you evaluate expressions from left to right, returning the last operand's result (which, in your case, isn't stored anywhere, and is perfectly valid).

Reference:

  • http://www.ecma-international/ecma-262/5.1/#sec-11.14
  • https://developer.mozilla/en-US/docs/JavaScript/Reference/Operators/Comma_Operator

The most obvious point of this is to allow for multiple expressions in a for loop:

for (let x=3, y=6; x < 10; x++, y++) {...}
                           ^^^^^^^^

That's the ma operator, the same operator that also allows for the examples you provided

return (x, y)
发布评论

评论列表(0)

  1. 暂无评论