Take the 2-minute tour ×

As the title says, whats the difference between

MyFunction = function() {



function MyFunction() {



Duplicate: http://stackoverflow.com/questions/336859/javascript-var-functionname-function-vs-function-functionname

share|improve this question

marked as duplicate by Greg Dec 17 '08 at 16:43


If you close saying duplicate, can you please comment with a link to what it's a duplicate of? –  singpolyma Dec 17 '08 at 16:44
I prefer adding them to the question - they're easier to find, I think –  Greg Dec 17 '08 at 16:49
@RoBorg fair enough. When I looked it wasn't anywhere was all :) –  singpolyma Dec 17 '08 at 17:09
dang, sorry about that. didnt spot the dupe. –  jdee Dec 17 '08 at 17:17

5 Answers 5

up vote 1 down vote accepted

The first form is actually a variable with an anonymous function assigned to it, the second is a declared function.

They're almost interchangeable, but have some differences: debuggers have a harder time with anons (because they lack a name) and the JS engine can directly access declared functions wherever they exist in the script, but anons can't be accessed until they have been assigned.

share|improve this answer

function myFunction(){} is the same as var myFunction = function myFunction(){}. If you just do MyFunction = function(){}, it's like MyFunction = function anonymous(){}.

The variables and functions have different scopes but the function can only be referenced outside the function via the variable name, which needs to be bound to the function.

function myFunction(){} binds the myFunction variable to a function that just happens to have the same name.

share|improve this answer
You'd think so, but there's a very subtle difference. As a very simple example (due to character limit) var foo = function(){alert(foo)}; f = foo; foo = 5; f() // alerts '5' var foo = function foo(){alert(foo);} f = foo; foo = 5; f() // alerts with our function –  olliej Dec 18 '08 at 3:29
And I would be right for your case: a function's own name is in its own scope, whereas the outer "foo" is in an outer scope. –  Mark Cidade Dec 18 '08 at 19:03

There's one subtle difference on Firefox, when you're declaring a window load event handler. Declaring the following in a script tag only works on Firefox:

function onload() {

You have to do it like this for IE, Opera, Safari or Chrome:

onload = function () {
share|improve this answer
isn't this a different issue? onload isn't a local -- its a member of window. your code is really saying "window.onload = function() { blah }, which is a different idea –  Jimmy Dec 17 '08 at 16:44
the first will work because there's effectively an implicit with(window) so it actually assigns to window.onload, and would be just as broken as the second form if it was declared "var onload..." - this is an absolutely horrible implementation of onload event binding though –  annakata Dec 17 '08 at 16:46
@Jimmy: Whether it's a different "issue" at the core or not, this is answering the question of whether there's "absolutely no difference at all" between the two declaration types. It seems that there is one odd scenario that there is. –  Ates Goral Dec 17 '08 at 16:50
@annakata: I agree that the first form is absolutely horrible. I'm just trying to illustrate a difference in behaviour that I had accidentally discovered while playing around with a unit test framework. –  Ates Goral Dec 17 '08 at 16:52
it's not a difference in behaviour because it's doing something else entirely - this is one of those "there is no spoon" moments –  annakata Dec 17 '08 at 17:00

here is one difference:

function Moose() {
   alert(arguments.callee.name); // "Moose"
var Cow = function() {
   alert(arguments.callee.name); // ""
share|improve this answer

I think you mean

var MyFunction = function() {

And in many cases, there is little difference.

The var syntax is sometimes useful when you are inside some sort of wrapping class and want to be sure of your namespace (like in greasemonkey).

Also, I believe the function-are-constructors stuff (with .prototype, etc) do not work with the var syntax.

share|improve this answer
