(ES6) ES6 (2016)

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:



Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21>  <22>  <23>  <24>  <25
Link to this page: http://www.vb-net.com/JavascriptES6/GL01.htm
<TAGS>  <ARTICLES>  <FRONT>  <CORE>  <MVC>  <ASP>  <NET>  <DATA>  <TASK>  <XML>  <KIOSK>  <NOTES>  <SQL>  <LINUX>  <MONO>  <FREEWARE>  <DOCS> <TRAVELS> <FLOWERS> <RESUME> < THANKS ME>