0

Below are 2 ways of declaring javascript functions.

Method 1:

function pollingLoop(req, res) {
    // Function Body
}

Method 2:

var pollingLoop = function() {
    // Function Body
}

What is the difference between the 2 functions?

user1496463
  • 409
  • 3
  • 13
user781486
  • 20,909
  • 45
  • 145
  • 253
  • 1
    The answer is _hoisting_. Check [this](http://elegantcode.com/2011/03/24/basic-javascript-part-12-function-hoisting/) out – Ivan Jul 29 '15 at 07:26
  • Duplicate.Check here http://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname – AkshayJ Jul 29 '15 at 07:27
  • 1
    Method 1 declares a named function that takes two parameters `req` and `res`. Method two declares a variable and assigns a reference to an anonymous function that does not define any named parameters. Method 1 allows you to call the function *anywhere* inside its scope of availability, even before you defined it (that is, above it in the code). This is called *hoisting*. In method 2, the variable is also hoisted, but not its value, so if you wanted to use the function you'd have to make sure its declaration is above the parts where it is called. – connexo Jul 29 '15 at 07:33

0 Answers0