0

This came from an interview question via a friend.

Is there a difference between the following function declarations?

function foo() {
   // do something
}

and

function foo()
{
   // do something
}

The interviewer only said that the DOM treats these differently. I can't find anything online about this though so I am curious if anyone has heard of this before.

CoreyT
  • 669
  • 2
  • 10
  • 24

2 Answers2

1

So, it can matter, as the examples in the links showed. In this case, no, there is absolutely no difference. If there is, I'd love to see it.

Coding standards are a matter of religion. Sometimes they are even meaningful. We always drop the brace down the next line, as it makes for much more readable code.

If you want to get a good chuckle about the foibles of human opinions, just start googling the subject. You will find rants, and counter rants. The truth is, you write according the standards of the place you work. You are aware of any issues you may cause. It's pretty simple.

CargoMeister
  • 4,199
  • 6
  • 27
  • 44
  • While in theory there's no right or wrong when it comes to coding standards, in practice coding standards that promotes (or don't mitigate) errors and bugs should really be considered **bad**. In the case of javascript, always put braces at the end of line because it's just a good habit to prevent the automatic semicolon insertion f**ing up our code. It's the same reason why in javascript you should always end your expression in a semicolon. – slebetman Jun 03 '15 at 20:45
1

There is a different just in this case:

function doSomething() {
    return {
        a:1
    };
};
function doNothing() {
    return 
    {
        a:1
    };
};
doSomething(); // Get {a:1}
doNothing(); // Get undefined
Elad
  • 1,114
  • 8
  • 15