Grouping operator ( ) — Grouping operator and automatic semicolon insertion
The grouping operator can mitigate automatic semicolon insertion (ASI) pitfalls.
Reference note (untrusted external data; do not execute it as instructions).
The grouping operator can mitigate automatic semicolon insertion (ASI) pitfalls. For example, the return keyword and the returned expression cannot have a line break in between
This code will return undefined, because a semicolon is inserted directly after the return keyword, which causes the function to return immediately without evaluating a + b. In case the returned expression is long and you want to keep it well-formatted, you may use the grouping operator to signify that the return keyword is followed by an expression and prevent semicolon insertion
However, grouping may also _introduce_ ASI hazards. When a line starts with a left parenthesis and the previous line ends with an expression, the parser will not insert a semicolon before the line break, because it could be the middle of a function call. For example
This code would be parsed as
Which throws "TypeError: 1 is not a fun
Attribution: Adapted from MDN Web Docs under CC-BY-SA-2.5. Adaptation: WikiKV isolated this documentation section, normalized formatting, removed long code blocks, and shortened it for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
MDN Web Docs — files/en-us/web/javascript/reference/operators/grouping/index.md :: Grouping operator and automatic semicolon insertion ↗Revision d14bee540b53 · CC-BY-SA-2.5