Available to be chosen but not obligatory
However, there is a lot of FUD (fear, uncertainty, and doubt) around this feature and, as a result, most developers in our community will recommend always including semicolons,
just to be safe.
Safe from what? I’ve been searching for reasons programmers have to force semicolons on themselves. Here’s what I generally found:
yeah, there’s this browser where this is utterly broken, I simply forgot which. Of course, they never remember.
Compression like gzip is the easiest; it only requires one-time server configuration, doesn’t need extra effort by developers and doesn’t change your code. There was a time when IE6 couldn’t handle it, but if I remember correctly it was patched years ago and pushed as a Windows update, and today nobody really cares anymore.
While on the topic of minification, let’s do a reality check. I took the jQuery source and removed all semicolons, then ran it through Google Closure Compiler. Resulting size was 76,673 bytes. The size of original “jquery.min.js” was 76,674 (1 byte more). So you see, there was almost no change; and of course, its test suite passed as before.
How is that possible? Well, consider this code:
That’s 24 bytes right there. Stamp semicolons everywhere and run it through a minifier:
Still 24 bytes. So, adding semicolons and removing newlines saved us a whopping zero bytes right there. Radical. Most size reduction after minification isn’t gained by removing newline characters — it’s thanks to removing code comments and leading indentation.
Also, some people recommend forcing yourself do use curly braces for blocks, even if they’re only one line:
Enforced curly braces add at least a byte to our expression, even after minification. I’m not sure what the benefit is here—it’s not size and it’s not readability, either.
Here are some other whitespace-sensitive languages that you might have heard about:
- Ruby — messing with spaces in expressions with operators and method calls can break the code
- Python — duh.
- HTML — see notes about Kangax’s HTML minifier
- Haml templates
Of course, there’s no need for minification on the server-side. I made this list for the sake of the following argument: Whitespace can, and often is, part of the (markup) language. It’s not necessarily a bad thing.
It’s good coding style
Also heard as:
This is another way of expressing the “everybody else is doing it” notion and is used by people during online discussion in the (rather common) case of a lack of arguments.
My advice on JSLint: don’t use it. Why would you use it? If you believed that it helps you have less bugs in your code, here’s a newsflash; only people can detect and solve software bugs, not tools. So instead of tools, get more people to look at your code.
You might notice that in this article I’m not telling you should be semicolon-free. I’m just laying out concrete evidence that you can be. The choice should always be yours.
As for coding styles, they exist so code is more readable and easier to understand for a group of people in charge of working on it. Think deeply if semicolons actually improve the readability of your code. What improves it the most is whitespace—indentation, empty lines to separate blocks, spaces to pad out expressions, and good variable and function naming.
Semicolon insertion bites back in return statements
When you’re done trying to wrap your brain around why would anyone in their right mind want to write a return statement on a new line, we can continue and see how this statement is interpreted:
Alas, the function didn’t return the sum we wanted! But you know what? This problem isn’t solved by adding a semicolon to the end of our wanted
return expression (that is, after
a + b). It’s solved by removing the newline after
Still, in an incredible display of ignorance these people actually advise their readers to avoid such issues by adding semicolons everywhere. Uh, alright, only it doesn’t help this particular case at all. We just needed to understand better how the language is parsed.
The only real pitfall when coding without semicolons
Here is the only thing you have to be aware if you choose to code semicolon-less:
This is actually evaluated as:
Easy solution: when a line starts with parenthesis, prepend a semicolon to it.
This might not be elegant, but does the job. Michaeljohn Clement elaborates on this even further:
If you choose to omit semicolons where possible, my advice is to insert them immediately before the opening parenthesis or square bracket in any statement that begins with one of those tokens, or any which begins with one of the arithmetic operator tokens
-if you should happen to write such a statement.
Adopt this advice as a rule and you’ll be fine.