var a = function(){return true;}
var a = function abc(){return true;}
Can anyone explain to me what the difference between these two are?
var a = function(){return true;}
var a = function abc(){return true;}
Can anyone explain to me what the difference between these two are?
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...
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:
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'