2

The following doesn't work as I would expect it to:

function test()
{
  // Returns undefined, even though I thought it would return 1
  return
  1;
}

Apparently, the value should be on the same line: return 1;. Why can I write things like

// Assigns 1 to foo just fine
foo
=
1;

...but the return statement doesn't work the same way?

dragonroot
  • 5,653
  • 3
  • 38
  • 63
  • 4
    `=` isn't a legal statement in itself, so a semicolon couldn't be automatically inserted there. `return;`, on the other hand, is a valid statement, so it *can* be terminated by an implicit semicolon, leaving the `1;` statement hanging. – BoltClock Apr 21 '14 at 13:29
  • possible duplicate of [Strange behavior of 'return' statement in JavaScript](http://stackoverflow.com/questions/7723492/strange-behavior-of-return-statement-in-javascript) – Felix Kling Apr 21 '14 at 13:37
  • See also: http://stackoverflow.com/questions/2846283/what-are-the-rules-for-javascripts-automatic-semicolon-insertion-asi – Tim Goodman Apr 21 '14 at 13:39
  • @BoltClock I think it's more the other way round... according to the spec semi-colons are inserted when an *illegal* token is encountered. So if it were illegal to follow `foo` with `=`, then a semi-colon would be inserted. E.g., I think a semi-colon would probably be inserted in the case of `var =`, even thought the resulting program would still be invalid. – Tim Goodman Apr 21 '14 at 13:46
  • `return` is a bit of a different case, since it's a restricted production -- which doesn't apply to `foo = 1` – Tim Goodman Apr 21 '14 at 13:51

4 Answers4

2

It's explicitly part of the language spec. If it were not, there would still be return issues:

if (something())  return
counter = counter + 1;

Without the semicolon insertion rule, that missing semicolon would trigger behavior that's (I'd argue) just as bizarre as what happens now with return statements split across a newline.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • +1 for illustrating the rationale behind making return a restricted production. I knew it was one, but had only ever seen examples where this gives the wrong behavior. – Tim Goodman Apr 21 '14 at 13:38
2

Javascript automatically inserts a semicolon after the "return" statement if the return expression is not on the same line.

JavaScript has a mechanism that tries to correct faulty programs by automatically inserting semicolons. Do not depend on this. It can mask more serious errors. It sometimes inserts semicolons in places where they are not welcome. Consider the consequences of semicolon insertion on the return statement. If a return statement returns a value, that value expression must begin on the same line as the return:

return
{
   status: true
};

This appears to return an object containing a status member. Unfortunately, semicolon insertion turns it into a statement that returns undefined. There is no warning that semicolon insertion caused the misinterpretation of the program. The problem can be avoided if the { is placed at the end of the previous line and not at the beginning of the next line:

return {
   status: true
};

Quoted from this post, citing JavaScript: The Good Parts by Douglas Crockford. Copyright 2008 Yahoo! Inc., 978-0-596-51774-8.

Community
  • 1
  • 1
DerStrom8
  • 1,311
  • 2
  • 23
  • 45
0

JavaScript will insert a semi-colon in certain cases to try to make an otherwise invalid program into a valid one. In particular, a return statement is an example of what's called a "restricted production" -- you aren't allowed to have a line break after return before the value, so it gets transformed into return; and the following value becomes a separate statement.

In the case of

foo
=
1

this is not a restricted production, and furthermore neither = or 1 are illegal tokens in that position, so there's no attempt to insert a semi-colon at the end of the preceding line.

Tim Goodman
  • 23,308
  • 7
  • 64
  • 83
0

JavaScript uses semicolon insertion! For every line you forgot to close it with a semicolon JS will insert it automatically! In this case:

return // auto semicolon insertion
{ // that's fine
    1; // yey here's one
} // ok, np
// bye bye

This means in JavaScript the use of { opening bracket is not, like in other languages, by developer's choice. You have to follow the rules:

return {
    1;
}
Tim Vermaelen
  • 6,869
  • 1
  • 25
  • 39