Q1.function f() {
...
}
var f = function() {
...
};
Which will work?
Both will work. The difference here is that one is a named function ( function f() ) while the other is a variable equal to a function ( var f = function() ).
You have to be careful when setting variables equal to functions. This will work:
var f = function(n) { console.log(n); };
f(3); // logs 3
But this will break, since the variable is defined after the call to it.
f(3); // what is f? breaks.
var f = function(n) { console.log(n); };
But normal functions work fine.
function abc(n) { console.log(n); }
abc(3); // logs 3
xyz(5); // logs 5
function xyz(n) { console.log(n); }
This is because the code is analysed before execution, and all functions are available to call. But setting a var equal to a function is like setting a var to anything else. The order of when it happens is important.