DEV Community

Discussion on: Var vs Let vs Const

Collapse
 
kepta profile image
Kushan Joshi • Edited

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

function whatUp(){
console.log('what up');
} 
whatUp();
Enter fullscreen mode Exit fullscreen mode

and

var whatUp = function(){
console.log('what up');
}
whatUp();
Enter fullscreen mode Exit fullscreen mode

are not the same thing, if you just move the whatUp to the top.

whatUp();
var whatUp = function(){
console.log('what up');
}
Enter fullscreen mode Exit fullscreen mode

will fail whereas the one below won't fail

whatUp();
function whatUp(){
console.log('what up');
} 
Enter fullscreen mode Exit fullscreen mode

This happens because in javascript functions are hoisted. But it doesn't happen for anonymous functions.

var whatUp = function(){
console.log('what up');
}
Enter fullscreen mode Exit fullscreen mode

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.
image

But if we use a non-anonymous function it will also give us the helpful function name.

Collapse
 
sethusenthil profile image
Sethu Senthil

Thanks for letting me know! I appreciate your detailed explanation and examples. I'll fix the article ASAP! Once again thanks!