15

I was seeing some Javascript code and I stumbled upon something like this:

function() {
    if(true) {
        var a = 5;
    }
    alert(a);
}

I was pretty sure this would output undefined but it didn't ? Can someone tell me why?

7
  • Just curious, what's the point of the if block anyway... Commented Jun 5, 2014 at 15:45
  • 4
    @pstenstrm — There's only one function involved here, and no statements outside it. Closures are irrelevant. Commented Jun 5, 2014 at 15:47
  • 2
    @pstenstrm Closure? There's no closure here. Commented Jun 5, 2014 at 15:48
  • @War10ck As I mentioned in the question, I stumbled upon something like this, not exactly this. The point is I saw a variable being used outside the scope of which it was defined. Commented Jun 5, 2014 at 15:48
  • @pstenstrm This has nothing to do with closures. This question has everything to do with how scoping is different in JavaScript (function-level) as compared to Java or C# (block-level). Commented Jun 5, 2014 at 15:55

4 Answers 4

14

JavaScript has function level scope, not block level scope.

The var statement is hoisted so your code is equivalent to:

function() {
    var a;
    if(true) {
        a = 5;
    }
    alert(a);
}

If JavaScript had block level scope, then it still wouldn't output undefined. Since a would be undeclared in the alert statement, you would trigger a reference error.

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

4 Comments

Also this is a syntax error, as it's an anonymous function not called.
@IanClark That's besides the point, this is probably part of a full statement.
Though I thought I'd mention as they didn't understand hoisting / scoping :)
For every Java/C# developer learning JavaScript, this 'scope' question is "A rite of passage".
3

The reason this works is a result of what is called hoisting. Hoisting moves the declaration of the variable to the top of the scope. So your function really looks like this:

function() {
 var a;
 if(true) {
  a = 5;
 }
 alert(a);
}

"Because variable declarations (and declarations in general) are processed before any code is executed, declaring a variable anywhere in the code is equivalent to declaring it at the top. This also means that a variable can appear to be used before it's declared. This behaviour is called "hoisting", as it appears that the variable declaration is moved to the top of the function or global code." - var MDN

1 Comment

+1 Upvote. This is a complete answer. Function-level scoping in collaboration with hoisting is the key reason. Well explained!!!
2

Variable definitions are moved to the top of the function (variable hoisting); there are no block level variables.

The compiler changes your code to

function() {
    var a;
    if(true) {
        a = 5;
    }
    alert(a);
}

Comments

2

JS doesn't have block scopes just function\global scopes.
In your case the var is declared with no value at the top of the function and then is assigned.

Here's a good tutorial on scopes.

1 Comment

Exactly, this is why.

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.