I tend to be a prolific user of semicolons in my JavaScript:
var x = 1;
var y = 2;
if (x==y){do something};
I recently noticed that I was looking at a lot of JavaScript that doesn't have semicolons following if statements. It then occurred to me that I don't even know the preferred syntax for semicolons in JS and after some googling learned (rather surprisingly) that there is no need for semicolons at all aside from splitting statements that are on one line.
So, the question...where did this habit of people using semicolons come from? Is it a remnant from some popular language that was in use at the time JavaScript came into play? Just good practice in general? Is it just me?
I'm probably going to stick with it for no other reason that it's nice when writing jQuery chains to easily spot the end.
UPDATE:
Thanks for all the answers, everyone! It looks like, to summarize things, the reason we see a lot of semicolons in JS even when not needed comes from various variables:
- older JS minimizer's would produce broken code if you did not insert semicolons
- many other languages use them, so it's a carried-over habit
- on occasion, a semicolon CAN change the logic
- some prefer to use them to make the code more human-readable
PHP uses semicolons at the end of lines, and that's why i use them in javascript usually.
Apart from code readability, the use of semi-colons comes into picture when you actually try to minify your code. After minification, if there are no semicolons in your code, javascript engine might fail to know which statement ends where as there will be no longer be any new lines in the code.
Whether to use semicolons or not is largely a matter of choice. However, in javascript, unlike many other languages, not using them can lead to unexpected results. For example,
will, because of javascript's automatic semicolon insertion, return nothing.
Semicolons mark the end of statements in JS, but I do not put semicolons after a close-brace, only after a statement within a brace and ALWAYS on a return statement.
In your example I dont find it necessary.
The C language uses semicolons at the end of statements, and also uses braces.
The habit comes from fear. Fear of "broken" js compressors (I've had some problems with Plone compressors with legacy js that didn't have the
var
statement for example), fear of possible broken browser implementations, the list goes on. So, why not just use;
everywhere and avoid all of these pitfalls?The problem with this approach (just always use semicolons without further explanation) is that you don't try to understand why are you doing it in the first place, you just do because someone said you to do it, and, IMHO, this can be harmful as well. And the broken compressors you had in the past are now fixed, and you keep inserting semicolons... breaking habits is hard. We just keep doing things because "we always did it".
Here is a great resource about this subject and the source of the quote. A great read.
... and the 2nd quote from a more optionated article, against always using semicolons. A great read as well.