tags:

views:

123

answers:

5

Is there any difference between

function MyFunc() {
    // code...
}

and

var MyFunc = function() {
    // code...
};

in JavaScript?

A: 

Defining it as a variable:
You can pass a function to another function.
You can redefine a function reasigning that variable.
You can return a function from a function.
...

seize
both can be passed as arguments, or assigned to other variables.
Aaron Qian
I was talking about callback functions, perhaps I must clarify that.
seize
+3  A: 

This article might answer your question : JavaScript function declaration ambiguity

(look at the comments, too, which might get some useful informations too)

Pascal MARTIN
Be careful with the comments; Several of them are incorrect.
Matthew Crumley
+3  A: 

Yes

Fabien Ménager
+1 I was just thinking the same.
Gumbo
this is a good Answer
mck89
A: 

There is no difference superficially, so you can use both format in your code.

To js interpreter it is different though.

First one is a named funciton.

Second one is an anonymous function that gets assigned to a variable.

Also, while debugging, you won't get a name of for the second function in stack trace.

Aaron Qian
+2  A: 

I know that a difference between them is that named functions works everywhere regardless you declare them, functions in variables don't.

a();//works   
function a(){..}

works

a();//error
var a=function(){..}

doesn't work but if you call it after the declaration it works

var a=function(){..}
a();//works
mck89