0

I was reading the difference between Function Declaration and Function Expression at here

What is the difference between a function expression vs declaration in JavaScript?

var functionName = function() {} vs function functionName() {}

I came to know about the difference that one is defined at parse time and the other one at run time. But my curiosity is regarding which to use and which not to use and why one will be preffered over other.

Community
  • 1
  • 1
Rocky Singh
  • 13,980
  • 28
  • 91
  • 142

2 Answers2

0

This is a detailed writeup on the topic.

FYI, you can have a functionName before the parentheses even in a function expression:

var functionName = function functionName() { ... }

One reason to use a form that has function name in it (between function and ()) is so that a debugger can helpfully tell you what function something occurred in, when displaying a stack trace.

One reason to use a function expression is so that you have more flexibility about putting the function into a namespace. So if you are using namespace to organize your javascript into modules, you may well need to use function expressions.

For those reasons, the above form (function expression with function name) could be considered the best of both worlds. But it has the disadvantage of being repetitive (DRY!).

LarsH
  • 25,732
  • 8
  • 77
  • 136
  • The link is good but there I was struggling to find the answer of this "But my curiosity is regarding which to use and which not to use and why one will be preffered over other." – Rocky Singh May 18 '11 at 20:05
  • Kangax's page that you link to recommends against (or at least, recommends caution) using named function expressions because they don't work properly in IE <= 8 (not sure about 9). – Tim Down May 19 '11 at 08:26
  • @Tim: yeah. There are a lot of pitfalls you have to tiptoe around. – LarsH May 19 '11 at 14:28
  • @Tim: yeah, it mentions quite a few pitfalls to avoid. I don't think the author's recommending against them exactly, but I would agree that the dangers can be very subtle, and in many cases it's not worth the risk. – LarsH May 19 '11 at 14:45
0

Really very little. function foo(x) is just shorthand for foo = function(x). Adding the var limits the scope as with any name in Javascript.

Charlie Martin
  • 103,438
  • 22
  • 180
  • 253
  • 1
    No. `function foo(x) {}` is not shorthand for `foo = function(x){}`. The first is a *function declaration* and the second is a *function expression*. They are different in two significant ways: first, the function declaration is available anywhere in the current scope while the function expression is not (a phenomenon commonly known as "hoisting"). Second, the `Function` object created by the function declaration has a name, while the `Function` object created by the function expression does not, which can make a difference while debugging. – Tim Down May 18 '11 at 23:18
  • @Tim SO can you please let me know which one to use ? – Rocky Singh May 19 '11 at 07:08
  • 1
    @Rocky: I use both. Function declarations for functions I'm going to use more than once and function expressions for one-shot functions that are used as, for example, event listeners or in the so-called Module pattern: `(function() { /* Private code goes here */ })();`. Ultimately, it usually doesn't matter much. – Tim Down May 19 '11 at 08:31
  • @Tim, if you made that last comment into an answer, it would be the best answer here. – LarsH May 19 '11 at 14:59