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:


Automatic Semicolon Insertion:
* Avoid semicolon insertion on return statements

Table Of Contents
11 Arrays
12 Objects
14 Classes
16 Map
17 Set
24 Loops
27 Date
29 Scope
30 AJAX
35 Cookies
41 JSON
44 Fetch
45 Modules
46 Screen
64 Console
68 Symbols
73 Modals
76 Events
81 Automatic Semicolon Insertion
86 Proxy
89 WeakMap
90 WeakSet
102 Tilde