Horje
What is Global Scope in JavaScipt?

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






Reffered: https://www.geeksforgeeks.org


JavaScript

Related
How to check if an object has a specific property in JavaScript? How to check if an object has a specific property in JavaScript?
What is Array.flatMap() in JavaScript? What is Array.flatMap() in JavaScript?
What is Object.defineProperty() in JavaScript? What is Object.defineProperty() in JavaScript?
What is Array.join() method in JavaScript? What is Array.join() method in JavaScript?
What is function scope in JavaScript? What is function scope in JavaScript?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
15