Sunday, September 13, 2009

Semicolons in javascript

So, after examining some Google code I found they use semicolons more for statement delimiting than termination. Last statements in a code block are not semicolon terminated, only preceding code lines are. This got me searching for the meaning of semicolon in javascript. I found many sites stating it is optional, and many sites which warn of the danger of following this practice.

Although the script interpreter will insert semicolons at the end of lines, this may break down if the script is stripped of all needless white space. Therefore a good rule of thumb seems to be to put semicolons at the end of lines to clearly indicate the end of statement. Google's practice of using semicolon as statement delimiters seems to be adequate as well.

On a related topic, i have also found some advice for properly formatting javascript code because the interpreter will insert semicolons where it thinks appropriate. This is something I was not aware of before and knowing this now will probably help debug some code down the road.

No comments: