The Semicolon

In JavaScript, the semicolon acts as a statement terminator. It basically says "This code statement is done." In the onMouseOver script, the semicolon was used because the effect I wanted to achieve through the Event Handler is finished:

...onMouseOver="window.status='Go to the Goodies Home Page'; return true">Click Here</A>

Question:

So why not write the code to a new line, like I did in the first Chapter 1 assignment, with the document.write statements? I did fine without using semicolons at all and the script worked.

Answer:

Each of those document.write statements sat on its own line and had only one function. This is a different story because the onMouseOver script has two code statements. First, there is the onMouseOver, and then that return true statement. That's why it's all on the same line, separated by a semicolon. JavaScript knows the two items are related, and now also understands where one stops and the other begins, thanks to the semicolon.

Good habit Even though that semicolon may not be needed, it is good practice to use one every time a line of code is ended. It helps to see quickly where the lines end, plus the world will inherit another awesome javaScript author!
Back Table of Contents Next