This happens because in javascript functions are hoisted. But it doesn't happen for anonymous functions.
varwhatUp=function(){console.log('what up');}
The example above is called an anonymous function (since it doesn't have a name), assigned to a variable whatUp. Anonymous functions are great for places where you quickly want to pass a function which doesn't really need a name, for eg. callbacks. But they are difficult to debug, because if they throw an error, stack trace would look something like this.
But if we use a non-anonymous function it will also give us the helpful function name.
Sethy really like your illustrations and the article overall. But I believe
Functions are actually Vars
is wrong.It might seem they are the same, but there are some minor details which are easy to overlook.
In your example
and
are not the same thing, if you just move the
whatUp
to the top.will fail whereas the one below won't fail
This happens because in javascript functions are hoisted. But it doesn't happen for anonymous functions.
The example above is called an anonymous function (since it doesn't have a name), assigned to a variable
whatUp.
Anonymous functions are great for places where you quickly want to pass a function which doesn't really need a name, for eg. callbacks. But they are difficult to debug, because if they throw an error, stack trace would look something like this.But if we use a non-anonymous function it will also give us the helpful function name.
Thanks for letting me know! I appreciate your detailed explanation and examples. I'll fix the article ASAP! Once again thanks!