The function below will return b is not defined because the compiler will look in the function and then into the global scope in search for a variable b.
However, I assumed that defining b without the word var would automatically create a global variable?
Can anyone explain the rules when omitting the word var?
function foo(a) {
console.log( a + b );
b = a;
}
foo( 2 );
console.logthere b is not defined.bbefore you've defined it, of you move the console.log to afterb=a;it will return 4