Some style preferences are subjective, but some have very good reasons for being the way they are. Here's a simple JavaScript function that returns an object...
function blah()
{
return
{
key: "value"
};
}
...except it returns undefined when invoked:
console.log(blah());
undefined
Can you spot the bug? With so little code, it should be obvious, right? Before reading on, stop for a minute and really try to find the error.
...
Figured it out?
...
The answer is that JavaScript has automatic semicolon insertion. That means there's effectively a semicolon on the same line as return. ASI is why, in JavaScript, you always put the curly brace on the same line. Sure, you could try to remember the ASI rules, but you're guaranteed to be safe if you just put your braces on the same line. And considering how much code a typical programmer writes, you are almost guaranteed to inflict an ASI bug on yourself if you don't do this.
AFAIK, return statements are literally the only place where brace style is affected by ASI.
var result =
{
key: "value"
};
return result;
works fine, plus it lets you more easily break on the return statement and verify/modify what will be returned when debugging. It would be kind of awkward to see braces like that in JavaScript, but a style guide could just ban returning object literals and make the ASI issue moot (at least regarding braces; you still have the other gotchas with forgetting a comma in a variable declaration, etc).
God I hate the term "semicolon insertion". It's a line oriented language, like shell or BASIC. You only need semicolons when you put multiple statements on a line (like a minifier does). Alas, like Ruby, JS does not require, or allow, an explicit line continuation, such as a backslash or ampersand.
I hate the asshole at Netscape who decided the browser scripting language had to be modeled after Java (C/C++, in other words), especially when it was clearly meant to be a functional programming language that worked with lists and property lists.
Man, I'm feeling "troll-ish" tonight. Not that I'm lying, just being blunt.
...
Figured it out?
...
The answer is that JavaScript has automatic semicolon insertion. That means there's effectively a semicolon on the same line as return. ASI is why, in JavaScript, you always put the curly brace on the same line. Sure, you could try to remember the ASI rules, but you're guaranteed to be safe if you just put your braces on the same line. And considering how much code a typical programmer writes, you are almost guaranteed to inflict an ASI bug on yourself if you don't do this.