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.
abc,bandccannot exist.