2

Is it good practice to start all JS files with a semi colon to account for any bad scripts included before it? Or don't bother?

Thanks

;(function(){ /* my script here */ })();

JackMahoney
  • 3,423
  • 7
  • 32
  • 50
  • Why would it be bad? Clearly you already know why it's done. – I Hate Lazy Dec 02 '12 at 23:47
  • 4
    In my opinion it's only good practice if you write a lot of faulty code. – adeneo Dec 02 '12 at 23:47
  • And how `;` would protect from something "bad"? – zerkms Dec 02 '12 at 23:49
  • @adeneo: That's not the point. It's meant to guard against a situation where you join your code into a single file with other code, perhaps external libraries, and the other code doesn't terminate with a `;`. – I Hate Lazy Dec 02 '12 at 23:50
  • Yeah, and that would be faulty code! – adeneo Dec 02 '12 at 23:51
  • Ftr do think this should stay open, is constructive and in the spirit of "good subjective, bad subjective" post that shows up. – djechlin Dec 02 '12 at 23:51
  • You said *"...if you write a lot of faulty code"*. Could be some other code joined into a single file, and it's a harmless guard. – I Hate Lazy Dec 02 '12 at 23:52
  • I'll agree that it does'nt do any harm, but there's no denying that it's a "fix" to fix something you or someone else did wrong in the previous code block. It's like sticking everything in try/catch blocks in case something is wrong. It sounds like a good idea, but it really is'nt. – adeneo Dec 03 '12 at 00:02
  • @adeneo: Yes, it's a fix, but if you're going to use the library and join it into a singe file, then you're going to have to apply the fix somewhere. Comparing it to putting all your code in a try/catch is not a reasonable comparison. This is an innocuous technique that deals with a specific problem. – I Hate Lazy Dec 03 '12 at 00:06
  • ...it's not like people who clutter their code with `typeof foo === "undefined"` or `.hasOwnProperty()` everywhere. This is one tiny character. – I Hate Lazy Dec 03 '12 at 00:08
  • I personally just love developers who use Yoda conditions, like `'object' === typeof foo`. I see a lot of people using the semicolon "fix" in plugins and stuff, and I don't really think it's neccessary, and if you have control over whatever scripts you're joining why not just add the semicolon **if it's missing** and not all the time as some kind of "insurance" in case you forget to close whatever code you wrote yesterday? – adeneo Dec 03 '12 at 00:13
  • @adeneo: I do hear what you're saying. I guess I just see it as an automatic way to add it. Otherwise you need to remember to do it every time you download a new version of the other script. Anyway, it's clearly a subjective matter, which is why I voted to close this one right away. :-) – I Hate Lazy Dec 03 '12 at 00:23

5 Answers5

2

Do bother, more and more people leverage the power of ASI and write semicolonless JavaScript. In semicolonless JS world that's the "rule", you put a semicolon before raw expressions, like ;(), or ;[] or ;//, as well as after 'use strict'; and omit them everywhere else. Raw expressions are not very common, except the typical IIFE. Even if you write JS with semicolons, that particular one is safe and will do more good than bad.

elclanrs
  • 92,861
  • 21
  • 134
  • 171
  • 1
    I've to say I don't get why the hate for semicolonless JS. From experience, having written JS with and without semicolons, the later is less prone to errors (if you follow the rule) and it looks cleaner. All the code will be minified eventually anyway... – elclanrs Dec 03 '12 at 00:35
1

not a good idea:

  1. hide errors
  2. stuff wont work on your site with no clear indication as to why
  3. see no reason to use faulty scripts.
Itay Moav -Malimovka
  • 52,579
  • 61
  • 190
  • 278
1

Instead, start all your scripts with "use strict"; which on some browsers will check your scripts for some error-prone practices, and interestingly I believe it will have a similar effect as the ; for closing any outstanding statements from faulty scripts included prior.

Jim Blackler
  • 22,946
  • 12
  • 85
  • 101
  • It wouldn't be a good idea to put `"use strict";` outside the IIFE, since it can break other code. – I Hate Lazy Dec 03 '12 at 00:00
  • @user1689607 check this http://stackoverflow.com/questions/2343608/in-ecmascript5-whats-the-scope-of-use-strict I believe "use strict" only affects the script in which it is defined. – Jim Blackler Dec 03 '12 at 00:14
  • Yes, but though it isn't explicitly stated in the question, the reason for using the leading `;` is for when you join different scripts into one file, which would put them in the same script. – I Hate Lazy Dec 03 '12 at 00:21
  • Welcome in the 20k club! – peterh Oct 08 '19 at 19:20
1

The leading semicolon is actually pretty useful if you split and decouple a lot of code into several javascript files, which you at some point, concatenate to create a production file.

It will simply help to avoid errors in constructs like

(function() {
}())

if all your files are wrapped in constructs like this, it fill fail without any semicolon, separating them. Other than that, there isn't much value in that pattern.

jAndy
  • 231,737
  • 57
  • 305
  • 359
0

I usually don't bother, but, if you want to be really safe, then do it. If you are using somebody else's library, I would probably do it, but, then again, it is your choice.

user982270
  • 116
  • 2
  • 10