4 ms·
Two areas that I can come up with: 1. Minification -- If you want to minify your JS, you might run into problems if you don't insert all necessary semicolons.
by shutter 18y ago
Two areas that I can come up with:
1. Minification -- If you want to minify your JS, you might run into problems if you don't insert all necessary semicolons.
2. Convention -- If you wrote js code with no semicolons and then showed it to other JS programmers, you'll get a funny look, a slap in the face, or a free copy of "Javascript for Dummies", depending on the context.
- timknauf 18y agoHmm, I hadn't considered the minification context - good point. That said, doesn't HTTP compression make minification largely unnecessary? As for convention: yes, I have indeed had some funny looks when asking this question in real life (no slaps yet, though). If you're working in a team, you obviously need to conform to one coding style; I'm coming from a mostly sole-coder perspective. But why does the convention persist? (Is that a strange question to ask?) Is it simply the weight of tradition, or are there other benefits?
- neilk 18y agoDoesn't HTTP compression make minification largely unnecessary? It depends. If you have really high traffic or you care about every last microsecond of responsiveness, minification is essential. Also, minifiers these days tend to know about scope and program structure and may even be able to figure out when you're referring to defined browser objects like 'window'. So you just get that much more bang for the buck when combined with other forms of compression. As for your other question, about convention: back in the dark days of the mid-90s Javascript was just the subset of scripts that both IE and Netscape would support. Omitting semicolons usually caused horrible bugs in one or the other. Also, most tools for formatting source code assume that newlines are not significant, and may wrap, split, or join lines in a way that breaks the logic. Anyway, most battles over source code standards end in the most explicit option winning. This case is no exception. Your question is fair, considering that ECMAscript is standardized now. In principle, you could switch to a newline-terminated style, thus saving a whole extra character per line. (But that would be eliminated if you minified). However, even today, I would guess that omitting semicolons risks breakage. Browser makers don't test much with newline-terminated scripts.
- timknauf 18y agoThanks for the considered response: this sort of background information is exactly what I was looking for.
- inimino 18y ago"1. Minification -- If you want to minify your JS, you might run into problems if you don't insert all necessary semicolons." Only if the minifier is broken. Some of them are (I don't want to name names since it's been a while since I checked). The relevant advice would be "don't use broken minifiers," not "don't use automatic semicolon insertion." "2. Convention -- If you wrote js code with no semicolons and then showed it to other JS programmers, you'll get a funny look, a slap in the face, or a free copy of "Javascript for Dummies", depending on the context." The correct response to which is to politely refer them to ECMA-262 section 7.9 and go on your way.
- timknauf 18y agoThe correct response to which is to politely refer them to ECMA-262 section 7.9 and go on your way. I'll let you know how that goes. I even have a particular person in mind... :^D