When defining a function, what is the difference between these 2 ways:
function t1() {}
var t2 = function() {}
Is t1 a function itself and t2 a reference to function?
When defining a function, what is the difference between these 2 ways:
function t1() {}
var t2 = function() {}
Is t1 a function itself and t2 a reference to function?
The first one is using the function statement, which is equivalent to doing this:
var t1 = function t1() {};
It's very similar to your t2 example, one difference being that t2 isn't named; It's an anonymous function stored in the t2 variable.
Remember that when using the named function statement (like t1), the var declaration is hoisted to the top of the scope.
That's why this example works, even though it looks like it's calling the function before the function is defined. The function gets hoisted up above the sayHello variable, and that's why it can be used.
The flipside is this example, showing that the t2 example doesn't work because the 'foo' function doesn't get hoisted to the top.