Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't a Javascript return statement work when the return value is on a new line?

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?

like image 774
Lion Avatar asked Dec 16 '11 00:12

Lion


People also ask

What happens when JavaScript reaches a return statement?

When a return statement is used in a function body, the execution of the function is stopped. If specified, a given value is returned to the function caller. For example, the following function returns the square of its argument, x , where x is a number. If the value is omitted, undefined is returned instead.

Can you return a return JavaScript?

You can return any valid JavaScript object: strings, booleans, objects, arrays, and even other functions.

Does JavaScript return last line?

No, a return statement is not necessary at the end of a void function (sorry for the C-terms there). If you want to exit a function early, however (say if a specific condition wasn't met), then you'll use a return statement, even if you're not returning a value.

Does return break the function?

A return statement ends the execution of a function, and returns control to the calling function. Execution resumes in the calling function at the point immediately following the call.


3 Answers

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.

like image 68
Alex Wayne Avatar answered Oct 04 '22 15:10

Alex Wayne


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/

like image 23
kubetz Avatar answered Oct 04 '22 14:10

kubetz


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).

like image 39
Anderson Carniel Avatar answered Oct 04 '22 14:10

Anderson Carniel