Phil Henry Mcboob
Phil Henry Mcboob

Reputation: 13

Why is a variable without the 'var' keyword not in the global scope in the chrome console?

Because I am not using the var keyword why is the variable 'local' not in the global scope in the chrome console?

function test(){
local = true;
};

console.log(local);//Uncaught ReferenceError: local is not defined

Upvotes: 0

Views: 118

Answers (2)

Pankaj Kumar
Pankaj Kumar

Reputation: 881

It is simply because the test function is never called so the local variable is never created due to which it throws error

enter image description here

Upvotes: 4

Cristian Penarrieta
Cristian Penarrieta

Reputation: 331

It will be available once test() is invoked

Upvotes: 1

Related Questions