0

Possible Duplicate:
JavaScript: var functionName = function() {} vs function functionName() {}
What is the difference between these 2 function syntax types

In JavaScript, we can define a function, which will be called at a later time, using one of the methods below. That is, using a named function and assigning an anonymous function to a variable.

function myAdd(a, b) {
    console.log(a + b);
}
myAdd(3, 2);

var mySubtract = function (a, b) {
    console.log(a - b);
}
mySubtract(3, 2);

Are they basically always identical? By identical, I mean no special contexts that might make them different. For example, it turns out multiple left-hand assignment has some subtleties that might lead to a different result depending on the context.

Community
  • 1
  • 1
moey
  • 10,587
  • 25
  • 68
  • 112

1 Answers1

3

The function declaration is hoisted (and can be used everywhere in the scope), the function expression will be available only after the assignment.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Does function _hoisting_ also apply to multiple files? For example, http://stackoverflow.com/q/10511845/583539 – moey Oct 19 '12 at 02:10
  • No, different scripts are executed independently. See your answers there – Bergi Oct 19 '12 at 07:20