54

I've only written a small amount of JavaScript that runs embedded in a Java application, but it was tested using QUnit, has been mixed, and I've not noticed any problems yet.

Is there some conventional wisdom whether to use semicolons or not in JavaScript?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
quamrana
  • 37,849
  • 12
  • 53
  • 71
  • 2
    I collected a list of cases where it is important to use a semicolon [here](http://stackoverflow.com/questions/25088708/what-rules-must-i-follow-to-write-valid-javascript-without-semicolons). Some arguments for omitting semicolons can be found [here](http://softwareengineering.stackexchange.com/questions/142086/why-the-recent-shift-to-removing-omitting-semicolons-from-javascript). There is a code style package called [standardjs](https://standardjs.com/awesome.html) which removes semicolons. – joeytwiddle Apr 02 '17 at 09:15
  • StandardJS doesn’t recommend to use semicolons https://github.com/standard/standard/blob/caa357395b48bc5e1cced3c85a5eb47bbf4aeac8/RULES.md#semicolons – Michael Freidgeim Feb 17 '23 at 23:16

8 Answers8

64

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
  • 74,572
  • 17
  • 113
  • 180
  • 7
    Huh? `function foo(){}foo()` works fine. semicolons are only needed after function expressions, not function declarations as in your example. – Crescent Fresh Dec 02 '09 at 18:16
  • 2
    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
    ?
    – annakata Dec 02 '09 at 19:52
  • 6
    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-while statement*, *continue statement*, *break statement*, *return statement*, and *throw statement* no semicolon insertion is made on **function declarations** (using the *function statement*). – Christian C. Salvadó Dec 03 '09 at 22:47
  • 1
    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 Dec 04 '09 at 15:01
  • 1
    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 Dec 04 '09 at 20:59
  • 1
    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 Dec 07 '09 at 20:17
  • 1
    @CrescentFresh, CMS: Please provide your alternative views as an answer. – bukzor Nov 25 '11 at 18:08
  • @bukzor: http://stackoverflow.com/questions/1834642/best-practice-for-semicolon-after-every-function-in-javascript/1834674#1834674 – Crescent Fresh Nov 26 '11 at 01:34
  • @CrescentFresh: You're implying that these are duplicates, but i think this question is much more general than that answer. – bukzor Nov 26 '11 at 05:04
  • +1 Always use semi-colons. In this day an age it may be considered a style choice/preference, but IMO they should always be used. – welbornio Feb 25 '14 at 19:21
  • Here's a great article that shows there are only like 6 easy to remember situations where you need to use semicolons: inimino.org/~inimino/blog/javascript_semicolons. IMO also learning these situations gives you greater understanding of how Javascript works, and is well worth the 30 minutes to read the article. – johnsimer Jul 04 '16 at 15:46
  • Also see [Douglas Crockford explanation about why use them](http://www.jslint.com/help.html#semicolon) – lifeisfoo Jun 21 '17 at 14:03
  • How can you neglect a semicolon when you don't need them? It works just fine in Pyton. – Cale McCollough Dec 13 '22 at 02:07
  • The original JS was very loose and allowed all sorts of code which was generally bad practice and led to subtle, hard to find bugs. Each new revision of JS is making it closer and closer to tighter languages, like C. I find the ASI troubling, and I think it should go away - it is one more vestige of the looseness that leads to bad code. For the record, semicolons ARE required in JS, it's just the ASI inserts them for you. One more reason to eliminate the ASI - CPU cycles. Why do people want their code to run slower? – Christopher Biessener Mar 21 '23 at 14:15
  • All these answers are so severely outdated. These days, we have formatters, linters, TypeScript, etc. Using the tools available, there is no situation in which you will be bitten in the ass because you forgot some semicolon. It's not like we're coding in Notepad. You do absolutely not need semicolons. It's just visual noise. – Audiopolis Apr 04 '23 at 23:45
17

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

Hank Gay
  • 70,339
  • 36
  • 160
  • 222
12

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
  • 295,876
  • 54
  • 310
  • 348
10

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 parenthesis, ).)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Philip Reynolds
  • 9,364
  • 3
  • 30
  • 37
9

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 (aside 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
6

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
  • 17,163
  • 8
  • 39
  • 53
  • 5
    Not really. Any modern, standard minifier works by using a lexer/parser and keeping the program's Abstract Syntax Tree in tact. If a minifier removes a line break token it will replace it with a grammatically equivalent semicolon token on the same line. (My first statement is somewhat false - minifiers also reduce the Abstract Syntax Tree, but still keep it logically equivalent). Check out this minification demo: http://esprima.org/demo/minify.html. Also it's easy to learn the situations in which you do need JS semicolons: http://inimino.org/~inimino/blog/javascript_semicolons – johnsimer Jul 04 '16 at 15:58
4

The semicolon triggers auto-indenting in my editor. It is a good enough reason for me to use it always.

And yes, consistency too.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sandesh247
  • 1,658
  • 1
  • 18
  • 24
2

The basic idea of semicolons is to tell the browser that you have just finished a command. You should use them.

jjclarkson
  • 5,890
  • 6
  • 40
  • 62