3
(function() {
            var a = 'g';
            function foo() {
                console.log(a);
                a = 7; //
            }
            console.log(foo(), a);
    }());

Can anybody explain the step by step execution and out put of this code sample. I got the out put as 'g undefined 7'

3
  • 3
    Yes, the step debugger. Commented Sep 2, 2015 at 11:54
  • I'm voting to close this question as off-topic because the answer can be obtained using a debugger Commented Sep 2, 2015 at 11:55
  • 1
    you aren't returning anything from your function foo which is why console.log(foo()) returns undefined Commented Sep 2, 2015 at 11:58

3 Answers 3

3

You are creating an anonymous function and executing right away (IIFE)

Then, in this scope function :

  • You declare an a var with value g
  • You declare a function named foo which has visibility on parent function scope (IIFE scope). So it can see the a var. This foofunction is not called right away.
  • In logstatement, foo is executed :
    • a is logged (value g)
    • a var in IIFE scope is changed to value 7
    • foo returns nothing
  • Is then logged :
    • foo returns value which is undefined (no return value)
    • a value, which is 7 after foo is executed.

So you have in your console :

g
undefined 7
Sign up to request clarification or add additional context in comments.

4 Comments

Without IIFE also I am getting the same result. OK I understood foo() will result "undefined" since there is no return statement.But how come 'g' value is accessible to within foo() function. If I add 'var' to 'a' in inside the foo () result will become "undefined undefined g". That means without var keyword variable 'a' will become global.
In foo, without var, ais coming from you parent scope (IIFE scope). If you add var to a in foo function, you "override" the avariable coming from parent scope (IIFE function), it is called "shadowing". The a var declared that way in foo function is only accessible in foo function (and would be accessible in hypothetical sub-functions of foo)
I suggest you to read this good online book : github.com/getify/You-Dont-Know-JS . The chapter corresponding to your question is in book 2, chapter 3 : github.com/getify/You-Dont-Know-JS/blob/master/…
OK Thanks for your suggestion. I am reading a blog 'Advanced JavaScript Fundamentals'-Lan Hill . While reading I tried to understand the concept by writing small functions with all possibilities.
0
(function() {
    ...
    ...
}());

this is just a way to declare an anonymous function and call it

var a = 'g';
function foo() {
     console.log(a);
     a = 7; //
}
console.log(foo(), a);

here u are declaring a variable a = 'g' in javascript if u declare a variable using the keyword "var" the scope of that variable si the function, that means that inside that function (also in sub function) everyone can use it.

then u are declaring a foo function, the foo function can see the variable a because foo is declared inside the previous function. console.log(a) just print the value of a so it will print 'g' and then change the value of a to 7.

after the declaration u call console.log(foo(), a), this means that u are executing foo and printing the return value of foo that is 'undefined' because there is no return statement, and then printing the value of a that after the execution of foo is become 7.

so the output is:

g
undefined 7

Comments

0

This is an ananymous self-invoked function wich is executed automatically without any call.

And the results/steps of execution are:

  • g is thrown with console.log(a);, because initially we have a="g".

  • undefined 7 is thrown with console.log(foo(), a);:

    1. foo() will return undefined because there's no return statement
    2. and a will return 7, because we did a=7;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.