Globalscope (GL01)
1: // In a browser environment (HTML file with a <script> tag)
2: var globalVar = "I'm global!"; // Declare with var (or without var in non-strict mode - not recommended)
3: let globalLet = "Also global (let)";
4: const globalConst = "Global const";
5:
6: console.log(window.globalVar); // Accessing through the window object (global object). Output: I'm global!
7:
8: console.log(globalVar); // Also works directly, because of var. Output: I'm global!
9: console.log(globalLet); // Output: Also global (let)
10: console.log(globalConst); // Output: Global const
11:
12: function myFunction() {
13: console.log(globalVar); // Accessing the global variable from inside a function. Output: I'm global! It works only for var.
14: }
15:
16:
Global Scope (Browser): In a browser, the global scope is represented by the window object. The main difference lies in the global scope (window in browsers, global in Node.js) and how modules are handled. NodeJS modules do not pollute global scope. In browsers, top-level variables declared outside functions become properties of the global window object, but only if var is used. Node.js modules have their own scope, and variables are not added to the global scope unless explicitly attached to global. I'm global! I'm global! Also global (let) Global const
Globalscope context:
ES6 context:
- (2024) Notes about JS Closures. #ES6
- (2024) Notes about Javascript asynchronous programming. #ES6
- (2022) Modern Javascript books #ES6 #Doc
- (2021) JS learning start point #ES6
- (2021) Maximilian Schwarzmüller Javascript lecture #ES6
- (2021) Javascript interview question from Happy Rawat #ES6
- (2021) Javascript tests #ES6
- (2016) New unique features of Javascript (updated). #ES6
Comments (
)

Link to this page:
http://www.vb-net.com/JavascriptES6/GL01.htm
|