-3

Is there any difference between declaring a Javascript function like this:

function myName(...)

and like this:

var myName = function(...)

I don't believe so, but...

Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453

1 Answers1

1

The first is a function declaration
You have given it a name.
It will be hoisted.

The second is a function expression
The way you wrote it, it's anonymous.
It will only be available after the line where it is defined.

Paul S.
  • 64,864
  • 9
  • 122
  • 138
  • I cringe at the term hoisted... why is such terminology not used in other languages – Esailija Aug 04 '13 at 13:54
  • @Esailija you might see it called "loop-invariant code" or "scalar promotion" when talking about other languages. It's usually something the compiler does as an optimisation. – Paul S. Aug 04 '13 at 13:58
  • Yes, that's my point. It means something **entirely different** in any other language. E.g. you don't say a method is hoisted in Java when you are able to call it before it's declaration, that would be ridiculous but in Javascript it's ok to confuse these terms (hoisting applies as its actual meaning as well when discussing optimizations in javascript) :P – Esailija Aug 04 '13 at 14:01
  • @Esailija it's the same process, it's just _JavaScript's_ compilers tend to be less "smart" and therefore what it happens to is more obvious and more relevant when coding for it. "Hoisted" just happened to be the word that caught to describe it, probably because it gets mentioned a little more often in _JavaScript_ than other languages, and the others are longer to remember/say. – Paul S. Aug 04 '13 at 14:05
  • multipass compilation and hoisting are not the same process at all, if that's what you imply – Esailija Aug 04 '13 at 14:09