0

Possible Duplicate:
JavaScript: var functionName = function() {} vs function functionName() {}

What is the difference between those two functions

function a()
{
    b=2;
    alert(b);
}
a(); 

and this function

var a=function()
{
    b=2
    alert(b);
}
a();

what is the main difference

Community
  • 1
  • 1
user1758424
  • 87
  • 1
  • 8
  • Note that there is also the concept of property accessors, which are getter and setter functions: https://johnresig.com/blog/javascript-getters-and-setters/ They use the special keywords `get` and `set` before a "function" name which is really the name of the property that has its access handled. – Andrew Oct 05 '17 at 19:13

3 Answers3

3

The main difference is that when you declare function:

function a(){
    // something...
}

it becomes accessible in the same scope even before the place in the code where it is declared.

But when you assign anonymous function to a variable:

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

it is not available before the assignement.

When the functions are created

It is a result of when the function is actually created. In first case it is created when the code is compiled, while in the second case the function is created when the interpreter reaches the line of assignment.

Test code

You can see the difference I mentioned above by executing the following code (jsfiddle):

try {
    a();
} catch(e) {
    alert('problem calling function a(): ' + e);
};
try {
    b();
} catch(e) {
    alert('problem calling function b(): ' + e);
};

​function a(){
    alert('function a() called');
};

var b = function(){
    alert('function b() called');
};​

You will see (as in mentioned jsfiddle), that a() function is called properly even before the actual declaration, but b() is not available before the assignment.

Tadeck
  • 132,510
  • 28
  • 152
  • 198
  • This is all the difference? so why should I use the second example? – user1758424 Oct 24 '12 at 21:27
  • I believe that either this could be treated as _all_ the difference, or other differences could be treated as a result of this difference. There is no reason to say one is superior to another in general - it all depends on what you need it for, the specific use case. In the second case you will be able to operate on it as if it was just a variable, with place where it is defined, deleted etc. In the first case. It really depends on where and how you want to use it. – Tadeck Oct 24 '12 at 21:55
0

The only real difference is that the second function has no name, and that the function a() {} one is hoisted.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

The difference is that function a() is defined at parse-time for a script block while var a=function() is defined at run-time.

<script type="text/javascript">
    // No errors occured;
    function a();

    function a(){
        console.log("Success");
    }
</script>

<script type="text/javascript">
    // An error will occured;
    a();

    var a = function (){
        console.log("Success");
    }
</script>