11

I'm curious what the precedence of the Spread and Rest operators are in Javascript: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator

I was trying to find them on MDN's Operator Precedence table (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table) but unless they are a subcategory of an existing operator type, I don't see them. I couldn't find any other obvious documentation about it.

Scotty Waggoner
  • 3,083
  • 2
  • 26
  • 40
  • 1
    It had been part of that table [since 2014](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence$compare?to=613269&from=612097) but I fixed it [last month](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence$compare?to=1346329&from=1346230) :-) – Bergi Feb 07 '18 at 05:10
  • @Bergi: thank you! I can’t believe someone added that to the table o_O but who knows, things were still in flux in 2014 – Felix Kling Feb 07 '18 at 15:52

1 Answers1

22

Spread syntax is not an operator and therefore does not have a precedence.

It is part of the array literal and function call (and object literal) syntax.

Similarly, rest syntax is part of the array destructuring and function parameter (and object destructuring) syntax.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 1
    I guess that was the source of my confusion. Thanks for the quick answer! Hopefully it is more Googleable now. I had someone mention it in code review and suggest parentheses and it made me question how it got evaluated. – Scotty Waggoner Feb 07 '18 at 06:14
  • Parenthesis? No, definitely not. – Bergi Feb 07 '18 at 06:20
  • 4
    I was trying to see if I can do this: `func(...args || [])`. It works – Sarsaparilla Jul 08 '18 at 00:26
  • 3
    @HaiPhan Yes, you can do `func(...anyExpression)` where the only limitation to *any expression* is that it must not be a comma operator expression (which would count as an argument delimiter instead). – Bergi Jul 08 '18 at 15:53