Open In App

What is Global Scope in JavaScipt?

Last Updated : 14 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In JavaScript, global scope refers to the outermost scope in a program, where variables and functions are accessible from anywhere within the code, including inside functions, blocks, or nested scopes. Variables declared in the global scope are known as global variables and are accessible throughout the entire program.

Example: Here, the variable globalVar is declared outside of any function, making it a global variable. It is accessible both within the myFunction() function and outside of it. When myFunction() is called, it logs the value globalVar to the console without any issues, as the variable is available in the global scope.

Javascript




const globalVar = "I'm in global scope";
 
function myFunction() {
  console.log(globalVar);
}
 
myFunction();
console.log(globalVar);


Output

I'm in global scope
I'm in global scope




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads