"normal" function declarations go up to the top of the field, so they are always available.
Variable declarations are also raised, but assignment is not performed until a specific line of code is executed.
So, if you do var foo = function() { ... }
, you create the variable foo
in the scope, and it is initially undefined
, and only later this variable gets the purpose of the anonymous function.
If "later" after you tried to use it, the interpreter will not complain about an unknown variable (in the end, it already exists), but it will complain that you are trying to call an undefined
function reference.
source share