1

I am setting up some data to do an AJAX post and the code looks like this.

var data = {}
data.someId= 3;
data.anotherId = 4;

and this works fine. But why don't I need a semi-colon at the end of the first line?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Aran Mulholland
  • 23,555
  • 29
  • 141
  • 228
  • 3
    This is the best resource on the topic of ASI (semicolons in JavaScript) that I know of: [JavaScript Semicolon Insertion: Everything you need to know](http://inimino.org/~inimino/blog/javascript_semicolons) –  Jun 19 '11 at 06:42
  • 3
    Every time you forget to add a semicolon, Douglas Crockford cries. – Scott Jun 19 '11 at 07:20
  • @Scottie *I never **forget** to add them. I just **don't** add them.* (Except in the case of a statement beginning with a `(` -- in which case I prefix said statement with a semicolon. Other cases of statements beginning with a terminal confusing to ASI generally indicates another problem, such as an expression used as a statement.) –  Jun 19 '11 at 09:06

6 Answers6

5

They are optional. You don't need any of those.

https://mislav.net/2010/05/semicolons/

Dave Powers
  • 2,051
  • 2
  • 30
  • 34
Connor Smith
  • 1,274
  • 7
  • 11
5

Because JavaScript has Automatic Semicolon Insertion.

I erroneously called it Automatic Semicolon Injection earlier, which kind of makes sense :P

The language requires them, but it preprocesses your script and tries to guess where they should go. This doesn't always work out, as you can see in pst's comment.

You should just define the semi colons yourself. Don't let JavaScript guess.

Community
  • 1
  • 1
alex
  • 479,566
  • 201
  • 878
  • 984
  • This example is actually *not a case for ASI*. The reasoning is *even without ASI* the issue would still exist -- `return` is a restricted production. A case showing where ASI "doesn't always work out" is: `x = foo\n(funtion () {...})()`, where it is likely not intended for the evaluation of `foo` to be invoked as a function. –  Jun 19 '11 at 06:38
  • @pst Sounds like you'd have a good answer to [this question](http://stackoverflow.com/questions/5232169/is-this-a-problem-with-asi-or-just-the-return-grammar) :) – alex Jun 19 '11 at 06:40
  • http://inimino.org/~inimino/blog/javascript_semicolons <-- The best resource I know of on ASI :) Is the use of "Injection" a subtle pun? –  Jun 19 '11 at 06:41
  • @pst That was my mistake, but it is kind of relevant :) – alex Jun 19 '11 at 06:44
0

Sometimes your Linter settings will trigger if you don't use semicolons in places where your Linter is set up to detect them. However, some semicolons are optional in JavaScript including the semicolons put just before a line-break. Thanks to automatic semi colon insertion (ASI).

ASI is just a set of rules for when semicolons are optional, it doesn't actually insert semicolons into your code.

Some semicolons ARE necessary. Sxamples of necessary semicolons include the head of the a for loop and do-while loops.

Also be careful because although omitting optional semi-colons is valid code some tools like minifiers are written to interpret those semicolons so it could throw an error with one of those tools.

Steph M
  • 2,152
  • 1
  • 12
  • 11
0

A semi colon for a single statement on a line is not required in Javascript.

maaudet
  • 2,338
  • 4
  • 20
  • 28
  • This depends on the ASI rules. ASI can be tricked. Imagine: `x = foo\n(function() {})()`, which will confuse ASI. –  Jun 19 '11 at 06:34
0

JavaScript actually doesn't require semicolons to determine the end of a statement. If no semicolon is present, it will use a new line as the end of the statement. The only time it's necessary to use a semicolon is if you want to put two statements on the same line, one right after another.

All this being said, it's best practice to always end your statements with semicolons To avoid confusion and to avoid bugs that could be painful to find.

djibouti33
  • 12,102
  • 9
  • 83
  • 116
  • It does require semicolons when the ASI rules do not cover the situation. This can be the case when a line begins with `(` or `[`, for instance. –  Jun 19 '11 at 06:36
0

Semi-colons are optional in javascript. But it is recommended to add it.

Community
  • 1
  • 1
Denis de Bernardy
  • 75,850
  • 13
  • 131
  • 154
  • I do not recommend this. The only way "to be safe" is to 1) know the language 2) be consistent (semicolons or not) :-) A counter-example of "being safe" with semicolons are the restricted productions such as `return`. –  Jun 19 '11 at 06:37
  • I'm not recommending anything, myself. I merely pointed to the SO poll on the topic. :-) – Denis de Bernardy Jun 19 '11 at 06:43
  • I am just challenging the use of "most" :) It sets up a "truth in answer" statistic. –  Jun 19 '11 at 06:44