I have just started teaching myself JavaScript --and programming-- and I am kind of confused about Function Expressions. Basically, I understand the point of Functions, but I am trying to learn why a Function Expression would be used in place of a Function. Any help would be really appreciated.
Asked
Active
Viewed 55 times
0
-
1possible duplicate of [var functionName = function() {} vs function functionName() {}](http://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname) – vinayakj Jun 11 '15 at 18:58
-
when you don't need to refer to a function later, you can use an expression. this is nice because functions in JS are not used just for callable subroutines, they provide scope and timing features as well. – dandavis Jun 11 '15 at 19:09
-
one point to consider... function functionName will fire immediately if its nested inside another function. – Dan Beaulieu Jun 11 '15 at 19:11
-
So from what I am to understand, function expressions are are used for code which only needs to be run once within a task, rather than being repeatedly called? – Art Hartoyan Jun 11 '15 at 19:15
-
1It's just like non-functions; sometimes we feel the need to name a value, and sometimes we just use the value directly. (Are you confused about why someone would write `alert("Hello!")` rather than `var msg = "Hello!"; alert(msg)`?) – ruakh Jun 11 '15 at 19:35