0
What is the difference between function name() & let name = function () ?
1.function name(){..........} 2.let name = function () {..........}
2 Answers
+ 4
its hard but simple part to explain in javascript.
the behavior are called hoisting, basicly before running any code javascript engine will take note of any declaration both variable and function.
number 1 is function declaration, so js engine will simply remember that as function.
number 2 though is different, its an expression. remember what i said earlier ? "javascript engine will take note of any declaration", javascript will only take note "name" as a variable. But not its initialization nor assigment, because that what variable declaration is just give it a name. it'll just be remember as a variable until the code reach the assignment part of the code. so variable "name" wont have anything in it, until line let name = function(){...} reached
+ 3
example.
foo();
function foo(){
...
}
will work
foo();
let foo = function(){
...
}
wont work
let foo = function(){
...
}
foo()
will work