0

I have this piece of code

var a = 5;
function woot(){
    console.log(a);
    var a = 6;
    function test(){ console.log(a);}
    test();
  };
woot();

i'm expecting 5 and 6 as an output, but i've got undefined and 6 instead.

Any thoughts?.

6
  • First thought: I hadn't expected 5. What are you asking for? Do you need it to be 5? Commented Apr 28, 2013 at 21:59
  • Yes, in the tutorial that i'm working on, it should be 5, but when i tried it, it gives undefined. Commented Apr 28, 2013 at 22:01
  • 1
    Can you show/link that tutorial please? If it tells you there should be a 5, it is wrong. Commented Apr 28, 2013 at 22:03
  • This is a classic demonstration of variable hoisting. I think you may be misunderstanding the tutorial. Commented Apr 28, 2013 at 22:04
  • It's smashing node book. Commented Apr 28, 2013 at 22:06

3 Answers 3

4

Variable declarations are hoisted to the top of the scope in which they appear. Your code is interpreted like this:

var a; // Outer scope, currently undefined
a = 5; // Outer scope, set to 5

function woot(){ // Function declaration, introduces a new scope
    var a; // Inner scope, currently undefined
    console.log(a); // Refers to inner scope 'a'
    a = 6; // Inner scope, set to 6
    function test(){ console.log(a);} // Refers to inner scope 'a' (now 6)
    test();
  };
woot();

When you declare a variable inside a function, that variable will shadow any variable with the same identifier that has been declared in an ancestor scope. In your example, you declare a in the global scope. You then declare another variable with the same identifier in the scope of the woot function. This variable shadows the a that you declared in the global scope.

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

6 Comments

I can't understand the difference, and what's the meaning of hoisted??. Besides, it still gives undefined
@user2224801: It means that the var a inside woot shadows (supercedes) the global variable a with a local variable a, even before the var line. The var line is lifted ["hoisted"] to the top of the function where it appears, as James shows in his rewrite. But it's only the declaration, not the initialization, that is hoisted, so when a variable is declared with var, its initial value is undefined, and it only gets another value as of an assignment (or initialization). More: Poor, misunderstood var
ahaaaa, so if i removed var a from the woot, this will solve the problem.
@Fadwa - Yes, if you remove var from the var a inside woot you will no longer shadow the outer a so any reference to a in there will be referring to the a you declared outside woot.
got it, The var statement defines a variable within the current scope (all of it, not just "from here on").
|
0

The variable declaration (var keyword) is hoisted in the scope of your woot function, making it a local variable (shadowing the global variable a). It will be initialised as undefined, and return that value until you assign to it.

Comments

0

at the time of:

function woot(){
console.log(a);

..the a doesnt exist yet! if you want to use the outer a you need to call it like:

console.log( window.a );

Remove the a you have already in function and you can use, relaxed now, that console.log(a); which will refer to outer one (since there's no in your function anymore)

Otherwise, use console.log( window.a ); to differentiate the two alphas.

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.