In Javascript, the left brace, {, should be at the end of the line that begins the compound statement.
|
1 2 3 |
function foobar() {
return { 'foo' : 'bar' };
} |
I recently watched Douglas Crockford’s, author of Javascript: The Good Parts, Google Talk this past week. Amazing talk, if you have not had the opportunity I have embedded it below. While watching this video I cam across a "gotcha" in Javascript.
Code style is a very passionate topic to many people. To me it really is you do it your way and I do it mine. And it also really depends on the language you are working in. I generally prefer to put my curly braces on a new line.
|
1 2 3 4 |
function foo()
{
// Stuff
} |
It turns out that in Javascript this is a very bad thing to do. The Javascript compiler will attempt to insert semicolons any where it decides requires them. It is good programming practice to always insert a semicolon at the end of each line of code, but Javascript is very forgiving in this manner. If a developer writes the following code:
|
1 2 3 4 |
return
{
'foo' : 'bar'
}; |
The compiler will insert a semicolon immediately following the return statement, therefore not returning the JSON value. This is a very difficult situation to debug if you do not understand what is happening in the background with Javascript.

