Skip to content

Instantly share code, notes, and snippets.

@pszponder
Last active June 20, 2021 03:19
Show Gist options
  • Save pszponder/1307a1a6ebfb223f1dcc76922c2d2536 to your computer and use it in GitHub Desktop.
Save pszponder/1307a1a6ebfb223f1dcc76922c2d2536 to your computer and use it in GitHub Desktop.
Global Scope
// Define Variables in the JavaScript Global Scope
var vGlobal = "I was defined in the global scope using the var keyword";
let lGlobal = "I was defined in the global scope using the let keyword";
const cGlobal = "I was defined in the global scope using the const keyword";
// Access the variables defined in the global scope from the global scope
console.log("Accessing vGlobal from global scope: ", vGlobal);
console.log("Accessing lGlobal from global scope: ", lGlobal);
console.log("Accessing cGlobal from global scope: ", cGlobal);
// Define a function to access the global variables inside a function
function myFunc() {
// Access global variables from within the function
console.log("Accessing vGlobal from within function scope: ", vGlobal);
console.log("Accessing lGlobal from within function scope: ", lGlobal);
console.log("Accessing cGlobal from within function scope: ", cGlobal);
}
// Call the function
myFunc();
// Access the global variables from within block scope
{
console.log("Accessing vGlobal from within block scope: ", vGlobal);
console.log("Accessing lGlobal from within block scope: ", lGlobal);
console.log("Accessing cGlobal from within block scope: ", cGlobal);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment