Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save sivaprabug/d6fd6c0ef4b0e2e15f1d to your computer and use it in GitHub Desktop.

Select an option

Save sivaprabug/d6fd6c0ef4b0e2e15f1d to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title>Hiding global Variables with local variables of the same name</title>
</head>
<body>
<script>
var myVar = "Global Variable";
function printing() {
var myVar;
console.log(myVar);
var myVar = "Local variable inside the function"
console.log(myVar);
// Window object means we can access the global variable inside the function
console.log(window.myVar);
}
printing();
console.log(myVar);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment