0

can anybody tell me whats the difference between this two examples:

//difference with this:
var x = function withName(a,b,c) {} //why i should declare a name here?

//and this
var y = function (a,b,c) {}
Clement Amarnath
  • 5,301
  • 1
  • 21
  • 34
  • 2
    From my personal experience: Function names help debugging when you read a call stack, but I found this huge explanation, with lots of background, that can explain that better: https://kangax.github.io/nfe/ – martinczerwi Oct 01 '15 at 16:09
  • 1
    @ClementAmarnath It's not a duplicate of that. This question is about "named function expressions", not function declarations. – Barmar Oct 01 '15 at 16:27

2 Answers2

1

According to the ECMA specification, a function can be written either through declaration or through expression,

Declaration is writing function as below,

function withName(a,b,c) {

}

With declaration, it is required to write an identifier which in this case is withName

Both the example that you have given are of function expression where it is not required to write the identifier i.e. withName as you can still call this function with the variable x or y

var x = function withName(a,b,c) {} 

var y = function (a,b,c) {}

However, the only difference here is that if you don't specify the identifier you are creating a anonymous funtion.

You can see this link for detailed explanation.

Community
  • 1
  • 1
Saumil
  • 2,521
  • 5
  • 32
  • 54
0

On this particular case, there is no difference. But in general, the fact that the function assigned to a variable has a name, makes it possible to invoke the function from inside the same function (recursion).

Andres
  • 10,561
  • 4
  • 45
  • 63