Understanding Scope: Global vs. Local in Javascript


Variables have either a global scope or a local scope.

Source Code

var globalVar = "I'm global";

function testScope() {
  var localVar = "I'm local";
  console.log(globalVar); // Accessible here
  console.log(localVar); // Also accessible here
}

testScope();
console.log(localVar); // Error: localVar is not defined outside the function
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments