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

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

6
  • possible duplicate of var functionName = function() {} vs function functionName() {} Commented Jan 29, 2014 at 21:03
  • Only one of those functions is anonymous. The other is a named function called abc. Commented Jan 29, 2014 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. Commented Jan 29, 2014 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. Commented Jan 29, 2014 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. Commented Jan 29, 2014 at 21:16

3 Answers 3

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...

Sign up to request clarification or add additional context in comments.

1 Comment

At least that's the way it's supposed to be... some implementations screw this up however :(
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

2 Comments

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.
Good point — nothing should be lost in minification by dropping the function names.
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'

5 Comments

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: 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.
@ScottSauyet thanks, I wanted to explain it, but you were faster.
@OverlappingElvis: And I would hope Kooilnc's updated code makes it even more clear.
@ScottSauyet Yes for sure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.