Possible Duplicate:
What are the rules for Javascript's automatic semicolon insertion?
JavaScript befuddles me with its implicit line termination. It's a very C-like language, except that ending lines in a semi-colon is often optional.
So how does it decide when to assume an end-of-line?
Consider this example:
var x = 1 + 2
-3 + 3 == 0 ? alert('0') : alert('3')
Punching that into an HTML file and opening it in Safari popped up 3
. If you stick a semicolon on the end of the first line, it changes to 0
.
The algorithms and logic are all straightforward; what interests me is by what criteria JavaScript decided, in this instance, not to assume an end-of-line after the first line. Is it that it only waits for an error scenario before assuming an EOL? Or is there a more definite criteria?
I'm very curious. I haven't researched this much; I want to see what the S/O community has to say about it. I always end my lines with semicolons anyway, but I have some JS compression code that trips on the semicolon issue from time to time when I inadvertently leave one out.
Edit
OK just to clarify what the actual question is here: Can anybody describe, in non-abstract terms, when JavaScript will and won't automatically insert semi-colons.
This is NOT a duplicate. I'm aware that the rules for autmoatic semicolon insertion are well established and concisely documented. They're also long winded and confusing because they are generally abstract. In my experience, high level programmers don't digest low level documentation as well as simple end results, which is what I'm looking for.
If no one is interested, then I'll have a crack at answering myself when I get a chance. In the meantime, please leave the guns in the holsters for a couple of days. Thanks.
Don't leave it to the compiler, always put semicolons at the end of statements.
The ECMA specification (ch. 7.9.1, page 26) states:
I think this implementation has to do with the second point where:
can be parsed as a single complete ECMAScript Program
Because it's not always clear how the parser will insert semi-colons, it's advisable to not leave it to the parser (i.e. always insert the semi-colons yourself).
In ch. 7.9.2 (Examples of Automatic Semicolon Insertion) of the same specs this example looks like your situation: