Semicolons
Unlike PHP, semicolons are not generally required by JavaScript if you have only one statement on a line. Therefore the following is valid:
x += 10
However, when you wish to place more than one statement on a line, they must be separated with semicolons, like this:
x += 10; y -= 5; z = 0
You can normally leave the final semicolon off, because the new line terminates the final statement.
Warning
There are exceptions to the semicolon rule. If you write JavaScript bookmarklets, or end a statement with a variable or function reference and the first character of the line below is a left parenthesis or bracket, you must remember to append a semicolon or the JavaScript will fail. So, if in doubt, use a semicolon.
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