Be Careful Where You Break Lines
A number of dynamically typed programming languages don’t care about semicolons and treat them as optional syntax. JavaScript is not one of them—semicolons are not optional. In some places a semicolon is critical, while in other places it’s more of a stylistic choice. However, if you leave out a semicolon where it’s required, JavaScript will not complain. That’s because JavaScript has this philosophy that it’s more fun to take revenge than to complain.
Merely placing a ; is not sufficient. We have to truly understand JavaScript’s automatic semicolon insertion (ASI) rules.
A valid program ends with a ;. If a given script does not end with a ;, then JavaScript inserts a ; automatically.
As the tokens are parsed ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access