I have this code:
function myFunction(){
alert("Hello");
}
And this other code:
var myFunction = function(){
}
What is the difference?
I have this code:
function myFunction(){
alert("Hello");
}
And this other code:
var myFunction = function(){
}
What is the difference?
The first is the normal way to declare a function in javascript. You call it by referring to its name, myfunction().
The second is an anonymous function that is stored in a variable as functions are first class citizens in javascript. The variable myfunction now holds the anonymous function.
Basically the first is a normal function while the second is a variable holding an anonymous function.
The first is a named function, which if you were to look at a stack trace, you'd see myFunction
when it was called.
The second is a variable set to an anonymous function. In a stack trace, this function would have an <anonymous>
as it's name, making it harder to trace when there are many anonymous functions.