To clear the confusion
What is Function declaration
// this is function declaration
function foo(){
// code here
}
OR
//this is ok, but without name, how would you refer and use it
function (){
// code here
}
to call it immediately you do this
function foo(){
// code here
}()
What is Function expression
// this is a function expression
var a = function foo(){
// code here
};
or
var a = function (){
// code here
};
in the second case you have created a anonymous function.you still have a reference to the function through the variable a
.so you could do a()
.
invoking function expression
var a = (function (){
// code here
}());
the variable a is stored with the result of the function(if you return from the function) and loses the reference to the function.
In both the cases you can immediately invoke a function but the outcome differs as pointed above.