5

Today I stumbled on this javascript snippet.

var x = 5, y = 6;
x
++
y
alert (x + " " + y);

I would like to know why this doesn't throw a syntax error and more why y is 7 at the end? What is the use of this strange snippet if there are any at all?

JSFiddle here

DarkBee
  • 13,798
  • 5
  • 41
  • 53
  • 2
    [What are the rules for Javascript's automatic semicolon insertion (ASI)?](http://stackoverflow.com/questions/2846283/what-are-the-rules-for-javascripts-automatic-semicolon-insertion-asi) – Givi Jan 18 '14 at 18:38
  • My guess would be that after the `x`, the parser isn't expecting anything in particular so when it sees the line end, it adds an automatic semi-colon. But, after the `++`, the parser is expecting another term so it keeps looking for that and finds the `y`. – jfriend00 Jan 18 '14 at 18:38

2 Answers2

7

This is due to automatic semi-colon insertion. Semi-colons are not optional in JavaScript. They simulate being optional by having the runtime add them for you.

The parser can only do so good a job at this. The basic algorithm is "if the line is a valid statement, then plop a semi-colon after it and execute it, if it's not, keep going onto the next line"

The parser turned that code into this:

var x = 5, y = 6;
x;
++
y;
alert (x + " " + y);

It's fashionable now to leave off semi-colons, but I still think that's a bad idea after years of coding in JS.

Matt Greer
  • 57,161
  • 16
  • 118
  • 122
  • +1 Posted just before me, but more detailed than I would have put :) – Ashley Medway Jan 18 '14 at 18:39
  • "if the line is a valid statement, then plop a semi-colon after it and execute it, if it's not, keep going onto the next line" That is quite simply wrong. – user123444555621 Jan 18 '14 at 18:57
  • 3
    *"The runtime can only do so good a job at this."* The runtime doesn't do it at all. It's done while the JavaScript is parsed. And it does its job just fine, there are however rules it follows, and so the *developer* may not do so good a job at following the rules. Of course, any developer that writes code like this has deeper issues. – cookie monster Jan 18 '14 at 19:12
  • @Pumbaa80 please elaborate. How is it wrong? It's a summary of a rather complex spec into one sentence. It will of course have some oversights and assumptions in it. – Matt Greer Jan 18 '14 at 19:58
  • 1
    @cookiemonster thanks, that is a valid point. Updating my answer. – Matt Greer Jan 18 '14 at 19:58
  • @MattGreer The opposite of what you're writing is true. Basically the parser will always try to combine two lines if they produce a grammatically correct statement. – user123444555621 Jan 18 '14 at 20:22
  • @Pumbaa80 if that was the case, wouldn't the result in this example be `x++`? – Matt Greer Jan 18 '14 at 20:44
  • I think @Pumbaa80's comment under the other answer explains this particular case. The trouble with a simple one sentence description is that it really can't possibly be accurate. I think your description is probably closer to what the Go language does for ASI, which would be much nicer and more predictable. We wouldn't have troubles with code like `var foo = "bar" \n (function() { /* IIFE */ })()` and `var foo = "bar" \n [/*array*/].forEach(function() { /* do it */ })`. I must admit, I lean toward the current "fashionable" semicolon elision. ;-) – cookie monster Jan 18 '14 at 21:53
6

I think, the cause is the Automatic Semicolon Insertion (ASI) of Javascript. The code is interpreted as follows:

var x = 5, y = 6;
x;
++y;
alert (x + " " + y);
Thomas Junk
  • 5,296
  • 2
  • 25
  • 39
  • 2
    The interesting part is that it is not interpreted as `x++;y;`, since the postfix `++` is explicitly listed as an exception ("restricted production"). – user123444555621 Jan 18 '14 at 19:02