Despite that semicolons in JavaScript are optional, there's strong advice against not using semicolons:
it minifies to var a=1,b=7,sum=a+b(a+b);
, which will result in the error number is not a function
. There are also other cases and this case.
Update: This bug wasn't resulting from minification, however, the next one is:
It makes your code vulnerable for bugs resulting from minification: Try:
var isTrue = true
function doSomething() { return 'yeah' }
function doSomethingElse() { return 'yes, dear' }
doSomething()
!isTrue && doSomethingElse()
minifies to:
var isTrue=true;function doSomething(){return "yeah"}function doSomethingElse(){return "yes, dear"}doSomething()!isTrue&&doSomethingElse();
which results in:
SyntaxError: Unexpected token !
It makes your code less readable and maintainable in terms of convenience: Using the semicolon has been rightfully established as good practice, and a trained JavaScript developer will be puzzled by code trying to evade the convention.
Another thing is, you have to ask yourself: What do you really gain omitting the semicolon?
- Clean Code? If you want JavaScript to not look like JavaScript, try CoffeeScript. But there are some misguided notions, which believe that pitfalls like the one mentioned above are cleared by «Easy solution: when a line starts with parenthesis, prepend a semicolon to it.». How is this clean code, and how does that help anyone reading your code?
Bottom Line: With minification, I would definitely try to use conventions of JSLint. I've seen some people finally linting their JavaScript code after hours of trying to fix a bug not happening in unminified, but in minified code. Don't put yourself into this misery; semicolons may be ugly at first sight, but they keep the bugs out.