function foo(x, y, z)
{
if (x)
{
a();
} else {
b();
c();
}
}
function foo(x, y, z) {
if (x) {
a();
} else {
b();
c();
}
}
1TBS is a variant of the (older) K&R (Kernighan and Ritchie) style. In K&R style, functions are written in Allman style and braces are omitted where they are not necessary, e.g. around single-statement then-cases.
function foo(x, y, z)
{
if (x)
a();
else {
b();
c();
}
}
return {
name: 'Jane'
};
If you formatted that in Allman style, you’d have:
// Don’t do this
return
{
name: 'Jane'
};
However, JavaScript’s automatic semicolon insertion [2] adds a semicolon after return, if it is followed by a newline. The above code is thus equivalent to
return;
{
name: 'Jane'
};
That’s an empty return statement, followed by a code block (not by an object literal!) whose insides consist of the label name and the expression statement [1] 'Jane'. At the end, there is the empty statement, a semicolon (;) on its own.
The return statement is one of the few cases where newline is significant in JavaScript: it works as a terminator for statements. In the future, newlines might become more significant in JavaScript [3].
if (x) return x;