views:

525

answers:

10

I've only written a small amount of javascript, that runs embedded in a java application, but tested using QUnit, has been mixed and I've not noticed any problems yet.
Is there some conventional wisdom whether to use semi-colons or not in javascript?

Duplicate of Do you recommend using semicolons after every statement in JavaScript?

+3  A: 

I'd say use them all the time; most code you'll encounter uses them, and consistency is your friend.

Hank Gay
+1  A: 

The basic idea of semicolons is to tell the browser that you have just finished a command. You actually don't need them at all if you put each instruction on a different line but it is good practice to put them in anyway.

jjclarkson
+3  A: 

Yo should read THIS previous question.

Luis Melgratti
+1 you beat me by a couple seconds
jmein
A: 

this is a duplicate

jmein
+9  A: 

Use them. Use them constantly.

It's far too easy to have something break later on because you neglected a semi-colon and it lost the whitespace which saved it before in a compression/generation/eval parse.

annakata
Huh? `function foo(){}foo()` works fine. semicolons are only needed after function expressions, not function declarations as in your example.
Crescent Fresh
You have something of a point. The example *can* work because modern javascript engines are very *very* good and insert virtual semi-colons where they *should* exist, but the example is supposed to fail by spec and will do so in older browsers, which is admittedly an ever-decreasing problem, but you must also bear in mind that one day we may experience an XHTML-esque move towards standards and are you still writing <br>?
annakata
That example is **not** supposed to fail by spec (see ECMA-262, 3rd Ed. Ch.13) it will do work in older browsers (tested on IE5.5, a 1999 browser), and it has nothing to do with **Automatic Semicolon Insertion** (ECMA-262 Sec. 7.9), that happens only for the *empty statement*, *variable statement*, *expression statement*, *do-whilestatement*, *continue statement*, *break statement*, *return statement*, and *throw statement* no semicolon insertion is made on **function declarations** (using the *function statement*).
CMS
For posterity's sake: http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf This answer is just flat out wrong.
Crescent Fresh
Frankly I'm baffled at the attention (to a closed year old question) and the hostility. The answer is strictly not "just flat out wrong" because the point is that using semicolons is a good thing, and this is the majority opinion by far. As for the spec, yeah looks like I'm wrong - I lazily went by 7.9.1.1 ("The offending token is }.") without checking the function grammars as well. I will say that I have run this in NN4, IE4 and IE5.0 *without success* though. Offending code removed, perhaps one or two of todays dozen downvotes can likewise be removed now?
annakata
FYI you got linked to from http://stackoverflow.com/questions/1834642/best-practice-for-semicolon-after-every-function-in-javascript/1834695#1834695 I just want to say that no hostility was intended. Even reading my comments I don't see it. Perhaps in your eyes a downvote is a hostile act, considering you rarely downvote anyone? For me it's just a way to get the poster to look at their answer a little more scrupulously than they otherwise would have, in order to make the answer better of course.
Crescent Fresh
+2  A: 

If you don't use them and then minify your code you can run into issues with all your code being on a single line and the browser doesn't fully grasp which command ends where.

Steerpike
+1  A: 

Use them. There are a few reasons why, most notably

  1. javascript minifiers / compressors
  2. exceptions to the rule that a new line is a new expression (e.g. terminating a line with a variable and starting the next one with a parantheses.)
Phil
+1  A: 

I always promote the use of semi-colons when writing Javascript. Often the interpreter will be able to infer them for you; but I have yet to see a reason (asides from laziness ;-)) why you would deliberately write your code in a less precise fashion than possible.

To my mind, if the structure of the code is obvious, it will be really clear where the semicolons go, such that you won't even have to think about it after getting in the habit (i.e. at the end of each line); on the other hand, if it's not immediately clear to you where the semicolon goes, then chances are the structure isn't the most obvious anyway, and explicit semicolons are needed there more than they would be elsewhere.

It also gets you into the habit of understanding and delimiting statements in your head, so you have a (admittedly marginally) better understanding of how your code might parse into an AST or similar. And that's got to be a good thing when debugging syntax errors.

Andrzej Doyle
+2  A: 

They are required by the ECMAscript standard, see section 7.9 - it's just that the standard defines some rules that allow them to be automatically inserted while parsing the script.

So always use them!

Paul Dixon
A: 

The semi colon triggers auto-indenting in my editor. Good enough reason for me to use it always.

And yes, consistency too.

sandesh247