Hello fellow programmers and students,
I have come across a code snippet I can’t quite figure out.
function foo() {
console.log( this.a );
}
var a = 2;
foo(); // undefined
Why such an output when ‘a’ is declared in the global scope and ‘this.a’ inside the function resolves to the global object??? Shouldn’t the output be ‘2’? And no, this is not in strict mode. I ran this code on VS Code.
The funny thing is, when I run the same snippet on browsers, it outputs ‘2’
If someone would be kind enough to clear this up for me, I’d be immensely grateful. Cheers.