1

I am trying to understand variable scopes in Javascript and a friend asked me this question. I could use some help here.

function abc(){
  var a = b = 10; // a is local, b is global variable. Right?
  c = 5; // c is global variable
}
d = 20; // d is global variable

console.log(b); // b is not defined error. Why? Isn't b a global variable?
console.log(c); // Again, Why is 'c not defined'.

I ran this code in chrome console. Shouldn't I expect 10 and 5 in console? It gives a 'b is not defined', 'c is not defined' error instead. Why does this happen if b, c are global variables? console.log(d) works just fine.

Here's a fiddle.

1
  • 1
    If you don't call abc, b and c cannot exist. Commented Sep 21, 2015 at 7:01

2 Answers 2

4

Why does this happen if b, c are global variables?

b and c are only created if you actually call abc. Merely defining the function does not magically execute its body.

function abc(){
  var a = b = 10; // a is local, b is global variable.
  c = 5; // c is global variable
}

// call abc to execute the statements inside the function
abc();

console.log(b); // 10
console.log(c); // 5

That said, implicitly creating global variables is not good still. Avoid globals if possible, and if not, explicitly declare them by assigning to the global object (window in browsers).

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

1 Comment

Thanks for clarifying this. I had wrongly assumed that simply declaring a variable without the var keyword makes it accessible anywhere. We either call that function explicitly, or use self invoking functions. And yes, I avoid global variables as much as I can. I just wanted to bring it up here for my understanding.
1

EDIT: This is why I love SO, you learn something knew even when you're a know it all and answer questions you clearly are not equipped to answer. Thanks to @FelixKling's clarification I have updated this to reflect that vars

So there's a little bit of a terminology confusion going on here:

Javascript has function level scope: as a and b are declared inside the function block they are local to the function they were declared within and are constrained to the function's scope.

Variables defined outside a function (or in a browser on the window object, in node on the global object), have globalscope.

So the var keyword doesn't actually have anything to do with global scope, scope is defined by where you declare a variable.

Taking your example:

function abc(){
  var a = b = 10; //a is local, b is global (see @FelixKling's answer)
  c = 5; // c is global as it is not prefixed with the `var` keyword 
}
var d = 20; // d is global 


console.log(a); // logs "10" to the console
console.log(b); // b is not defined
console.log(c); // 'c not defined'.
console.log(d); // logs 20 to the console. 

4 Comments

Variables declared inside the function block using var keyword are local
b and c are not declared. They are assigned to and JavaScript will make them global.
It does implicitly declare them, but in global scope (not very useful ;) )
Yeah I just editted my comment as I realised I hadnt finished that sentence :)

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.