Avoid semicolon insertion on return statements

suggest change

The JavaScript coding convention is to place the starting bracket of blocks on the same line of their declaration:

if (...) {

}

function (a, b, ...) {

}

Instead of in the next line:

if (...)
{

}

function (a, b, ...) 
{

}

This has been adopted to avoid semicolon insertion in return statements that return objects:

function foo() 
{
    return // A semicolon will be inserted here, making the function return nothing
    {
        foo: 'foo'
    };
}

foo(); // undefined

function properFoo() {
    return {
        foo: 'foo'
    };
}

properFoo(); // { foo: 'foo' }

In most languages the placement of the starting bracket is just a matter of personal preference, as it has no real impact on the execution of the code. In JavaScript, as you’ve seen, placing the initial bracket in the next line can lead to silent errors.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents