Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created June 20, 2012 18:24
Show Gist options
  • Save cowboy/2961408 to your computer and use it in GitHub Desktop.
Save cowboy/2961408 to your computer and use it in GitHub Desktop.
javascript: function declaration vs expression
function foo(state) {
function bar() { return 1; }
return state ? bar : null;
}
foo(false) // null (bar is created but not used)
foo(true)() // 1 (bar is created and returned)
function foo(state) {
return state ? function() { return 1; } : null;
}
foo(false) // null (the anonymous function isn't even created)
foo(true)() // 1 (the anonymous function is created and returned)
@leobalter
Copy link

Thanks. \o/

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