Object-return syntax in JavaScript

5

Using the Chrome console I used the following code (valid):

function foo()
{
    return {
        prop: "some value"
    };
}

When changed, the syntax style is no longer valid ( { played to next line):

function foo()
{
    return 
    {
        prop: "some value"
    };
}

Is this a JavaScript syntax rule or a bug implementation of browsers? I tested this on Internet Explorer too (same behavior).

    
asked by anonymous 31.07.2014 / 19:55

2 answers

3

The syntax is:

  

return [expression];

And says about "expression":

  

A return expression. If omitted, undefined will be the return.

MDN Font

This implies that the expression, variable, object, etc. are in the same line as the code. If you change lines the interpreter will think you have return isolated and make return undefined .

    
31.07.2014 / 19:57
2

Complementing the @Sergio response, you need to put it on the same line because the semicolon, in Javascript, is optional.

When you do:

return 
{
    prop: "some value"
};

The interpreter understands how to:

return;
{
    prop: "some value"
};
    
31.07.2014 / 23:18