41

Consider the following JavaScript:

function correct()
{
    return 15;
}

function wrong()
{
    return
          15;
}

console.log("correct() called : "+correct());
console.log("wrong() called : "+wrong());

The correct() method in the above code snippet returns the correct value which is 15 in this case. The wrong() method, however returns undefined. Such is not the case with the most other languages.

The following function is however correct and returns the correct value.

function wrong()
{
    return(
          15);
}

If the syntax is wrong, it should issue some compiler error but it doesn't. Why does this happen?

Ivar
  • 6,138
  • 12
  • 49
  • 61
Lion
  • 18,729
  • 22
  • 80
  • 110
  • 4
    possible duplicate of [What are the rules for Javascript's automatic semicolon insertion?](http://stackoverflow.com/questions/2846283/what-are-the-rules-for-javascripts-automatic-semicolon-insertion) – Pekka Dec 16 '11 at 00:47
  • 1
    This is one of the reasons that I put parens around my return values. – jfriend00 Dec 16 '11 at 00:50
  • The result is 15, not 10 as show. – Anderson Carniel Dec 16 '11 at 00:50
  • Yes edited. it was written by mistake. – Lion Dec 16 '11 at 00:52
  • Crockford has the anser. Just watch: www.youtube.com/watch?v=hQVTIJBZook#t=30m39 – Šime Vidas Dec 16 '11 at 01:03
  • 1
    The syntax is not wrong, it's valid just not expected. You just have an unreachable statement `15` after a return statement in the code. – Raynos Dec 16 '11 at 01:05
  • @jfriend00 But if the expression was on the next line the semicolon would still be inserted, no? – Dave Newton Jan 14 '12 at 20:23
  • @DaveNewton - when you use parens with the return statement where the opening paren is on the same line as the `return`, the JS interpreter knows not to end the `return` statement until it finds the closing paren regardless of line boundaries. When you don't use parens, it has to either encounter a semicolon or guess based on line boundaries. IMO, explicitly telling it is generally better/safer than letting it guess. If you put the opening paren on the next line after the `return`, then it wouldn't work because the interpreter would have already terminated the `return` statement. – jfriend00 Jan 14 '12 at 20:30
  • @jfriend00 Oh, you didn't say opening on the same line. I understand the rules, but thanks. – Dave Newton Jan 14 '12 at 20:47

3 Answers3

59

Technically, semi colons in javascript are optional. But in reality it just inserts them for you at certain newline characters if it thinks they are missing. But the descisions it makes for you are not always what you actually want.

And a return statement followed by a new line tells the JS intepreter that a semi colon should be inserted after that return. Therefore your actual code is this:

function wrong()
{
    return;
          15;
}

Which is obviously wrong. So why does this work?

function wrong()
{
     return(
           15);
}

Well here we start an expression with an open(. JS knows we are in the middle of an expression when it finds the new line and is smart enough to not insert any semi colons in this case.

Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
  • 3
    No, semi-colons are officially part of the syntax and there's noting in the syntactic productions of the language that would suggest that semi-colons are optional. It's just that the standard defines the behavior if they're omitted. So, it's more like a special case... it's like an add-on to the standard. – Šime Vidas Dec 16 '11 at 00:56
  • 1
    By optional, I mean that the script is parseable without them. As opposed to many other languages like C or Java that throw errors at compile/evaluation time when semi colons are absent. I did not mean to encourage the lack of semi colons in javascript. If fact, just the opposite. – Alex Wayne Dec 16 '11 at 01:01
  • 1
    It's the word "officially" that bothers me. Omission is tolerated, but semi-colons *are* part of the syntax - officially. – Šime Vidas Dec 16 '11 at 01:05
  • Fair enough, how about "technically" then :) – Alex Wayne Dec 16 '11 at 01:10
  • 1
    How about "in practice"... However, note that semi-colons aren't optional in all situations, only in certain ones. – Šime Vidas Dec 16 '11 at 01:15
5

If there is nothing after the return statement on that line then ; will be inserted there which will result in returning without any values => return value is undefined.

See: http://lucumr.pocoo.org/2011/2/6/automatic-semicolon-insertion/

kubetz
  • 8,485
  • 1
  • 22
  • 27
2

The command line of the javascript can not be broken by line breaks. But arguments of functions can be broken, not highly recommended (done in your example).

Anderson Carniel
  • 1,711
  • 2
  • 16
  • 22