Javascript Tutorial
Javascript Tutorial Introduction to Javascript JavaScript Code Editors & IDE JavaScript Hello World Example Javascript Syntax and Rules syntax_rules javascript_identifiers JavaScript Keywords & Reserved Words javascript_variables JavaScript Const JavaScript let vs var vs const Data Types in JavaScript JavaScript String Template Literals & String interpolation in JavaScript Tagged Templates in JavaScript String to Number in JavaScript Number Data Type in JavaScript NaN in JavaScript JavaScript Number Min & Max & Safe Values JavaScript EPSILON & Floating point precision Infinity in JavaScript JavaScript Bigint BigInt Vs Number in JavaScript Boolean Data Type in JavaScript Undefined in JavaScript Null in JavaScript Null vs Undefined in JavaScript JavaScript Operators Arithmetic Operators in JavaScript Unary plus & minus operators in JavaScript Increment & Decrement Operators in JavaScript Comparison or Relational operators in JavaScript Strict Equality (==) Loose Equality (===) in JavaScript Ternary Conditional Operator in JavaScript Logical Operators in JavaScript Bitwise Operators in JavaScript Assignment Operators in JavaScript Nullish Coalescing Operator in JavaScript Comma Operator in JavaScript Typeof JavaScript Operator Precedence in JavaScript JavaScript if, else & nested if statement Switch Statement in JavaScript While & Do While Loops in JavaScript For Loop in JavaScript Break statement in JavaScript Continue Statement in JavaScript Arrays in JavaScript Array Constructor in Javascript Sparse Array Vs Dense Array in JavaScript How to merge Arrays in JavaScript Array Methods in JavaScript Functions in JavaScript Function Parameters & Arguments in JavaScript JavaScript Default Parameters Pass by Value and Pass by Reference in Javascript Function Expression in Javascript Nested Functions in JavaScript Immediately-invoked Function Expressions (IIFE) JavaScript Callback Functions Arrow Functions in JavaScript Arguments Object In JavaScript Rest Parameters in JavaScript Objects in Javascript Create Objects in JavaScript JavaScript Object Properties Computed Property Names in JavaScript Object Literal in JavaScript Constructor Function & New Operator in JavaScript Delete Operator in JavaScript hasOwnProperty in JavaScript Using Getters and Setters in Javascript DefineProperty in JavaScript JavaScript Property Descriptors Enumerable, Writable & Configurable Object Destructuring in JavaScript Variable Scope in JavaScript Hoisting in JavaScript Lexical Scope & Closures in JavaScript This in JavaScript Global Object, Window & Globalthis in JavaScript Call function in Javascript Prototype In Javascript Prototype Inheritance in JavaScript Instanceof Operator in JavaScript Spread Operator in JavaScript

Global Object, Window & Globalthis in JavaScript

The Global object in JavaScript exposes useful variables & functions. It is known as the window in the browser & global in NodeJs. You can access the globalThis to access the global object irrespective of the environment in which JavaScript is run.

Global Object

The JavaScript has a built-in object, which we call a global object which always exists in the Global Scope. The global object exposes useful variables and functions. It also exposes a variety of information about the environment in which code is running. Since it is part of the global scope, we can access it anywhere in the program

The global object has different names in different environments. For Example in a browser, the window object is the global object. In NodeJs call it global object

Window Object in the Browser

The window object is the global object in the Browser. It contains several useful methods & properties.

For Example, the alert method comes from the window object. You can invoke it as shown below.

            
 window.alert("Hello")
                
        
    

You can also invoke it using shorthand. JavaScript automatically looks for it in the global object, which is the window object

            
alert("Hello")
                
        
    

Any variable we create using the var keyword in global scope is added to the global object by JavaScript. In the following example foo variable and window.foo points to the same variable.

            
var foo = "foo";
 
console.log(foo)  //foo
console.log(window.foo)  //foo
 
console.log(foo === window.foo); // Returns: true

                
        
    

Note that the let & const global variables do not become part of the global object.

            
let foo = "foo";
 
console.log(foo)  //foo
console.log(window.foo)  //undefined
 
console.log(foo === window.foo); // false
  

                
        
    

The alert, clearInterval, clearTimeout, setInterval, setTimeout & scrollTo are some of the functions that available in the window object

Global Object in NodeJs

Node JS allows us to run JavaScript applications without using a browser. Even it has a global object and it goes by name global. The window object is not available under NodeJS.

globalThis

The global object has different names in different environments. This creates a problem when we try to run the same JavaScript code in different environments. This is where globalThis property comes into the picture.

The globalThis is a global variable, which contains global this object. The global this object always points to the global object irrespective of whether the code runs in the browser, Node JS, or in any other JavaScript environment.

Hence we can always access the global object using the globalThis variable.

You can also use the global this object to access the global variable. But this will be undefined in Modules and inside functions running in strict mode.