What is the difference between the following two examples?
setInterval(myFunc, 100);function myFunc() { alert('asdf'); }setInterval(myFunc, 100);var myFunc = function myFunc() { alert('asdf'); }- 2If you would have just typed the title of your question into the StackOverflow search box, you would have gotten many answers, like this one:stackoverflow.com/questions/5403121/…user113716– user1137162011-06-23 02:41:58 +00:00CommentedJun 23, 2011 at 2:41
- There is only one declaration above, the rest are assignments of function expressions.RobG– RobG2011-06-23 05:25:44 +00:00CommentedJun 23, 2011 at 5:25
var myFunc = function myFunc() { alert('asdf'); }=> create a variable named myFunc. No wait, create a function named myFunc. Now set the variable to = myFunc the function. Now get rid of the function.cwallenpoole– cwallenpoole2011-06-23 05:53:47 +00:00CommentedJun 23, 2011 at 5:53
4 Answers4
According to ECMA standard, the first example is a functionstatement while the second is a functionexpression. According to Javascript a function statement counts as a definition, which means in the first example it is visible through the entire function (or script if it's not in a function). But in the second example,var myFunc will not have the value offunction myFunc until the second line, and therefore setInterval will be passedundefined.
The only syntax difference between function statements and expressions is that statements are not included in are larger expression: eg:(function foo() {}) is an expression, whilefunction foo() {} is a statement.
NB: I believe old IE (pre 9?) treated all function expressions as definitions.
To expound on this answer, consider the following code:
<script language="javascript"> alert(A); alert(B); function A() {return "A value";} var B = function B(){ return "B value";} alert(A); alert(B); </script>this will alert (in order):
- Function A()...
- undefined
- Function A()...
- Function B()...
5 Comments
setInterval part, for the same reason you gave. But when I tried it (in IE) it worked. Even if thevar myFunc part is hoisted above, doesn't the actual assignment happen inline there after thesetInterval? I'm confused.function myFunc() {...} setInterval(...); var myFunc = myFunc;Both samples are essentially doing the same thing. In the second example, you can also use a named function with a different name or an unnamed function.
var myFunc = function myOtherName() { alert('asdf'); }or
var myFunc = function() { alert('asdf'); }They are all the same.
Comments
In the first example:
> setInterval(myFunc, 100);> > function myFunc() { alert('asdf'); }The function declaration is processed before any code is executed, somyFunc exists as a local parameter whensetInterval is called.
The second example:
> setInterval(myFunc, 100);> > var myFunc = function myFunc() {> alert('asdf'); }works for exactly the same reason:myFunc is declared usingvar and therefore exists as a local variable whensetInterval is called.
Edit
Ooops! It doesn't work. The value ofmyFunc is evaluated whensetTimeout is called, and at that pointmyFunc hasn't bee assigned a value so an error results. Changing the value later doesn't affect the value held bysetTimeout.
Finally, there is no such thing as a "function statement". ECMA-262 definesFunctionDeclaration andFunctionExpression in §13, there is no other kind of function.
Comments
in the first example, you actually assign a newly created variable "myFunc" with a function data type. So, "myFunc" is the name of the variable, and function is the data type. The first one is the same as this:
var myFunc = function(){alert('asdf');}I have never seen anything like in the second example. I dont know if its even a valid way to declare a function...
1 Comment
Explore related questions
See similar questions with these tags.


