2
var a = function(){return true;}
var a = function abc(){return true;}

Can anyone explain to me what the difference between these two are?

David Newcomb
  • 10,026
  • 3
  • 42
  • 56
  • possible duplicate of [var functionName = function() {} vs function functionName() {}](http://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname) – matth Jan 29 '14 at 21:03
  • Only one of those functions is anonymous. The other is a **named** function called `abc`. – pete Jan 29 '14 at 21:04
  • This is *not* a duplicate of that question (although I am sure it is a duplicate), as *both* of the forms here are `FunctionExpressions` - one just has a name. – user2864740 Jan 29 '14 at 21:11
  • @user2864740, the second answer explains what this question asks (along with the accepted answer). It's not exact, but it tells me there wasn't any research done before posting. – matth Jan 29 '14 at 21:14
  • @making3 The answer does *not* answer this question. And while there are many related questions (and very likely a duplicate or two - although to the OPs credit, none of the related questions are duplicates), it still doesn't make that question a duplicate of the question currently proposed as such. Without knowing the term "named function expression" it's relatively hard to find this construct on SO, even if a trip to the specification or MDN reference would have cleared things up. – user2864740 Jan 29 '14 at 21:16
  • @making3 Whoops, you're correct - although I still maintain that it's not a good duplicate as the information is caught up secondarily, and not as the function of the primary question. There are [some answers there](http://stackoverflow.com/a/338053/2864740), [and](http://stackoverflow.com/a/14175010/2864740) (but not the accepted one) that does answer the question - it's a very long answer, though. – user2864740 Jan 29 '14 at 21:22

3 Answers3

4

The difference here is that here

var a = function abc(){return true;}

You are naming an anonymous function. This name abc is available only for the internal scope of the function.

UPDATE

It is true though that some implementations don't respect the convention... don't expect IE8 to work with it...

axelduch
  • 10,132
  • 2
  • 26
  • 49
3

Both of your examples are just function expressions — meaning that you're assigning an expression (which happens to be a function) to a variable. The difference is that one is anonymous, while the other is what's called a "named function expression". http://kangax.github.io/nfe/ has a great overview on what the differences between function declarations, expressions, and named expressions are. The short version:

  • These things are, for the most part, interchangeable in many situations
  • If you do a lot of debugging, having named instead of anonymous functions can make your call stack easier to read
  • However, there exist bugs in certain JavaScript implementations involving the use of named function expressions
OverlappingElvis
  • 611
  • 6
  • 20
  • Just to add to your answer, I find for larger projects it is handy to name the function (the second line) for debug purposes (things like the Chrome console will show the name if there is an error), but then having a minifier/cleaner go through and strip the names for the production releases. – samanime Jan 29 '14 at 21:18
  • Good point — nothing should be lost in minification by dropping the function names. – OverlappingElvis Jan 29 '14 at 21:19
2

The second function is a named function expression. It may be useful for recursion, e.g.

// named
var a = function abc(v) { console.log(v); return v>1 && abc(v-1) || v;}
//                                                      ^ name abc is known
   ,b = a
a(3); //=> 3,2,1
b(4); //=> 4,3,2,1
a = function (v) {console.log('value = '+ v);};
b(3); //=> 3,2,1

// versus
var a = function (v) { console.log(v); return v>1 && a(v-1) || v;}
//                                                   ^ using a here
   ,b = a
a(3); //=> 3,2,1
b(4); //=> 4,3,2,1
a = function (v) {console.log('value = '+ v);};
b(3); //=> 'value = 3'
Scott Sauyet
  • 37,179
  • 4
  • 36
  • 82
KooiInc
  • 104,388
  • 28
  • 131
  • 164
  • Note that using named function expressions isn't necessary for recursion — since `a` is available in the scope of the inner function, you can do `a(v - 1)` in your example. – OverlappingElvis Jan 29 '14 at 21:19
  • @OverlappingElvis: But notice that this works with a different reference to the function, the `b` reference in the example. Even if the original `a` reference was overridden, this technique would continue to work, whereas if you'd recurred on the `a` it would have broken down. – Scott Sauyet Jan 29 '14 at 21:28
  • @ScottSauyet thanks, I wanted to explain it, but you were faster. – KooiInc Jan 29 '14 at 21:31
  • @OverlappingElvis: And I would hope Kooilnc's updated code makes it even more clear. – Scott Sauyet Jan 30 '14 at 19:17
  • @ScottSauyet Yes for sure. – OverlappingElvis Jan 30 '14 at 20:11