0

What is the difference between function name() & let name = function () ?

1.function name(){..........} 2.let name = function () {..........}

11th Sep 2020, 7:44 PM
Arghya Ray
Arghya Ray - avatar
2 Antworten
+ 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
11th Sep 2020, 8:45 PM
Rei
Rei - avatar
+ 3
example. foo(); function foo(){ ... } will work foo(); let foo = function(){ ... } wont work let foo = function(){ ... } foo() will work
11th Sep 2020, 8:47 PM
Rei
Rei - avatar