Skip to content

Instantly share code, notes, and snippets.

@gaurish
Created November 7, 2011 11:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaurish/1344753 to your computer and use it in GitHub Desktop.
Save gaurish/1344753 to your computer and use it in GitHub Desktop.
JavaScript Function Declaration Patterns
// Style #1
function foo() {
//do something
}
//Style #2
var foo = function(){
//do something
}
//Style #3
var foo = function bar(){
//do something
}
@netroy
Copy link

netroy commented Nov 7, 2011

Style 1. is a statement/declaration that creates a function named foo with a reference called foo
Style 2. is an expression that creates an anonymous function & assigns it to a reference called foo
Style 3. ia an expression that creates a function named bar & assigns it to a reference called foo

Style-1 is hoisted, so you can use the function before the actual definition.
foo() before style 2 or 3 will give an error.
Also during function calls & error-stack generation the function name is used.

Extensive info at http://stackoverflow.com/questions/336859/javascript-var-functionname-function-vs-function-functionname

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment