Are there any reasons, apart from subjective visual perception and cases where you have multiple statements on the same line, to use semicolon at the end of statements in Javascript?
Yes. Internet Exploder will die horribly when omitting them.
It might also create trouble when minifying the Script.
Apart from that: Code Convention.
EDIT: adding these for reference:
As Douglas Crockford suggests -
Put a ; (semicolon) at the end of every simple statement. Note that an assignment statement which is assigning a function literal or object literal is still an assignment statement and must end with a semicolon.
Because JavaScript does nasty things to you when it guesses where to put semicolons. It's better to be explicit and let the interpreter know exactly what you meant than it is to let the idiot box guess on your behalf.
References:
- http://www.webmasterworld.com/forum91/521.htm
- http://www.howtocreate.co.uk/tutorials/javascript/semicolons
- http://robertnyman.com/2008/10/16/beware-of-javascript-semicolon-insertion/
...and a cast of thousands.
Technically speaking, your code will never work in obfuscators and/or minifiers; otherwise, because it is a part of the language.
Automatic semi-colon insertion is a gross handicap that almost everyone agrees should have never been included in the language.
Because
- Debugging the subtle bugs that occur when you don't is a waste of time you could be spending doing something cool
- Minifiers/packers/compressors rely on it
- It makes it clearer to someone maintaining the code later what you intend
- Not all code maintainers understand the rules for automatic insertion well enough to maintain code with them left out
- Not all implementations get the rules for automatic insertion quite right
If you asked, because you come from a Python background: The difference is:
in Python you shouldn't terminate lines with anything, but are allowed to use the semicolon, if you must
in JavaScript you should terminate the lines with a semicolon, but are allowed (PDF, page 25, point 7.9) to omit it, if it's unambiguous
yep, it's better to let javscript know than to let it decide for a variety of reasons, but browser compatibility is the most prominent one.