O'Reilly logo

Learning PHP, MySQL, JavaScript, and CSS, 2nd Edition by Robin Nixon

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Semicolons

Unlike PHP, JavaScript generally does not require semicolons 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 off the final semicolon, because the newline terminates the final statement.

Warning

There are exceptions to the semicolon rule. If you write JavaScript bookmarklets, or you end a statement with a variable or function reference and the first character of the next line 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.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required