1

Possible Duplicate:
What are the rules for Javascript's automatic semicolon insertion?

I know you can chain js (or jquery) on differnt lines like so:

$('div')
    .filter(':first').hide().end()
    .find('span').css('opacity', .5)
    ...;

And the engine doesn't put semicolons in which would break it. On the other hand if I do this:

return
    i ? 1 : 2

It puts a semicolon in and always return undefined. What are the guidelines for when it does what?

Community
  • 1
  • 1
qwertymk
  • 30,576
  • 24
  • 107
  • 179
  • The JavaScript parser will only place semi-colons if it encounters an error... – Šime Vidas Nov 28 '11 at 01:40
  • 1
    If you want a _really_ general guideline, it would be "Never put a linebreak between the `return` keyword and the expression being returned. For non-return statements don't worry." Yes this is oversimplifying things, but it will cover you almost all of the time. – nnnnnn Nov 28 '11 at 01:43
  • @ŠimeVidas - Good point. I guess I use `throw` so rarely in JS that I didn't think of it. (But then I did say I was giving a _really_ general oversimplified guideline, so...) – nnnnnn Nov 28 '11 at 01:56

4 Answers4

2

EDIT: It looks like Javascript adds semi-colons at new lines wherever it could make sense. Your first example would not be valid javascript if the second line began with '.' so it assumes it is one line, whereas in your second example, each line could potentially be valid js. For example:

return
1 + 2;

returns undefined, while both:

return 1 +
2;

and

return 1
+ 2;

return 3 because at least one line does not make valid js.

Godwin
  • 9,007
  • 4
  • 36
  • 55
  • 2
    This is a feature, not a bug. It is the stupidest feature in any widely-used programming language. – SLaks Nov 28 '11 at 01:41
  • Watch some Crockford. He uses that example a lot `:)` – Šime Vidas Nov 28 '11 at 01:42
  • Wow, and all this time I had no idea. – Godwin Nov 28 '11 at 01:46
  • @Godwin If you put a line break between `return` and the expression, the JavaScript parser will put a semicolon after `return` - this is the reason why `undefined` is returned in your demos. This is not a bug, it is defined in the standard. – Šime Vidas Nov 28 '11 at 01:47
1

There is a good explanation at http://inimino.org/~inimino/blog/javascript_semicolons. Quoting from it:

3. When a "restricted production" is encountered and contains a line terminator in a place where the grammar contains the annotation "[no LineTerminator here]", then a semicolon is inserted.

[...]

Restricted productions are those in which a line break cannot appear in a particular position, so if a line break appears there, it will prevent the program from parsing in that way, though it may still parse another way.

There are five restricted productions in the grammar, they are the postfix operators ++ and --, continue statements, break statements, return statements, and throw statements.

[...]

Note that return statements can contain linebreaks within the expression, just not between the return token and the start of the expression.

In other words, return ... is a specific construct wherein, if you put a line-break after the return, JavaScript will insert a semicolon. There are several other such constructs as well; but aside from these, JavaScript will only ever insert a semicolon if there's no way to make sense of your code without one.

Community
  • 1
  • 1
ruakh
  • 156,364
  • 23
  • 244
  • 282
  • @downvoter: Care to explain why? – ruakh Nov 28 '11 at 18:40
  • (Actually, @downvoter, never mind: everyone else, except for Mario, gave a completely wrong answer. I assume you're simply under the same misapprehension.) – ruakh Nov 28 '11 at 18:42
  • Strange how the worst answers get all the upvotes and are picked as the correct one, when the topic is about Javascripting. The quality of programmers, sadly, has really degraded in the web-development job, mostly thanks to jQuery coders I guess. –  Nov 30 '11 at 00:20
  • @Mario: I'd prefer to think that this specific error is a common misconception among JavaScripters, rather than that JavaScripterkind is going to Hell in a handbasket. This confusion is an understandable one, certainly. (And it's mostly harmless, when you think about it. The worst that can happen, if you believe that JavaScript inserts semicolons at every line-break where it can, is that you'll defend against it even in cases where you don't need to. Slightly uglier/less-idiomatic code, maybe a few more bugs as a result . . . but I've seen much worse confusions than this.) – ruakh Nov 30 '11 at 00:45
  • @Mario: For what it's worth, I've upvoted your answer. It's still at -2, but at least your net reputation from it is positive. :-) – ruakh Nov 30 '11 at 00:46
0

In your first example, the subsequent lines are invalid syntax when used alone.

In your second example, the second line is a valid (though useless) statement.

For more precise rules, see the spec, which goes on for three pages.

SLaks
  • 800,742
  • 167
  • 1,811
  • 1,896
  • 2
    The moral of the story: use semicolons. – Dave Newton Nov 28 '11 at 01:41
  • @SLaks: Two people have downvoted it, and I can only speak for one of them. The reason that *I* downvoted it is that, despite being the "accepted" answer, it's completely wrong. (Well, technically, each of the first two paragraphs makes a correct statement; but their purpose is to imply something that is completely wrong.) Something like `+ 1;` is a valid statement, but `return 1 ((newline)) + 1;` will *not* trigger semicolon insertion; it is equivalent to `return 1 + 1;`, but if your comment were correct, it would be equivalent to `return 1; + 1;`. – ruakh Nov 29 '11 at 15:35
  • @DᴀᴠᴇNᴇᴡᴛᴏɴ: That's a pretty useless moral, since the chief problem with semicolon insertion is that it happens even if you use semicolons everywhere that you should. (Not as often as SLaks's answer implies, but still.) The real moral is that, as "C++ Pitfalls" puts it, "Even language features that you never use can bite you!" (http://www.horstmann.com/cpp/pitfalls.html) Or, alternatively, that language designers shouldn't design languages to insert semicolons. ;-) – ruakh Nov 29 '11 at 15:40
0

This is better described as ending the statement parsing than inserting semicolons. In JS, semicolons are purely decorative.

The parser stops when it has a complete statement. The behaviour with return is exactly what you should expect, since return without an argument is a legal statement. It seems to be the only case that might get you into trouble if you're not careful because JS can't read your mind.

Putting the return argument on a separate line would be pathological even if it parsed right.

Marc Thibault
  • 1,598
  • 1
  • 12
  • 13
  • This is not true. If it stopped when it has a complete statement, it would stop at `$('div')`. As other answers have noted, it also needs to consider the next line. – David Wolever Nov 28 '11 at 16:33