$(function () {
       function foo() { return true; }
       log(bar());                        // getting error
       var bar = function() { return true; };   


$(function () {
        function foo() { return true; }
        log(bar());                        // working 
        function bar() { return true; };  

in above snippets log is my custom function to log the result.

closed as off topic by ChrisF Jun 11 '12 at 11:02

Questions on Software Engineering Stack Exchange are expected to relate to software engineering within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Please ask coding questions on Stack Overflow. If you create an account there, I'll migrate this question for you – ChrisF Jun 11 '12 at 11:03

In the second, the function bar() is hoisted to the top of your function block (all functions declared in a block using this syntax are hoisted to the top of the block) and thus exists before you call it in log(bar()). In the first, the variable bar is not set to your function until after your call to log(bar()).


In your first code block bar is a function variable that obeys scope. In the second code block is a globally declared function because it is not placed inside a scoped variable.


In the first, you have called an variable as an function.

var bar=function() {return true;};

try this in first one.

Not the answer you're looking for? Browse other questions tagged or ask your own question.