scope
var have a function scope.
hoisting – bring any sort of declaration including named functions on the top most scop possible.
vars bring the scope to the first line of the function. This happens when var is declared not being assigned. That is why you will get undefined instead of error
Let
uses block scope.
only used in whatever is being declared aka the block scope.
hoisting doesnt exist with let.
Const
scope – block scope like let.
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();
and
var whatUp = function(){
console.log('what up');
}
whatUp();
are not the same thing, if you just move the whatUp
to the top.
whatUp();
var whatUp = function(){
console.log('what up');
}
will fail whereas the one below won’t fail
whatUp();
function whatUp(){
console.log('what up');
}
This happens because in javascript functions are hoisted. But it doesn’t happen for anonymous functions.
var whatUp = 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.