Are there semicolon insertion dangers with continuing operators on next line?

I started seeing people changing code from things that looked like the first form to the second, and warning about "automatic semicolon insertion"

That is rubbish. When you have an operator (on either line), there will be no ASI - see What are the rules for JavaScript's automatic semicolon insertion (ASI)?

Since both styles of placing the operator will work, both are commonly accepted and it boils down to personal/style-guide preference. You named some arguments already, once you have chosen either stick to it for consistency.

There is nothing "dangerous" about either, and honestly you shouldn't need to care about ASI. People get bitten by it only because a) they don't like semicolons and expect automatic insertion, but the next line is a syntactically valid continuation or b) they write object/array literals after a return statement in Allman style.


If you take some syntactically valid line and punctuate it with line breaks, automatic semicolon insertion will not apply (except in the narrow case of return, throw and very few other statements, listed below). ASI only occurs when there is absolutely no other way to interpret the code. Certainly, there is a way to interpret your multiline code as a single statement, because it is valid as a single line. In short, ASI is generally a tool of last resort in the parser's attempt to understand the program.

To cite ES5, the first case of ASI detailed in the spec occurs...

  1. When, as the program is parsed from left to right, a token (called the offending token) is encountered that is not allowed by any production of the grammar...

But that case is naturally eliminated, because you had a grammatically valid line before you injected a newline into it. Thus, this case of ASI cannot apply to your case because it depends upon a span of code that is not syntactically valid without semicolons. You don't have that here.

(The other two cases don't apply either; the second case applies to the end of a program and the third case applies to continue, break, return, throw, and postfix ++/-- operators.)

The common problem people have with ASI occurs when an author has two lines which he expects will stand separately, but those two lines happen to cause no grammatical problem when understood as a single line. That case starts with two lines and they accidentally become one. Your cases is the inverse: you start with one line; it does not accidentally become two.