Table of contents
1.
Introduction: 
2.
Getting Started:
2.1.
window object in the Browser
2.1.1.
Access Global Variables
2.1.2.
Access Global Functions
3.
Frequently Asked Questions
4.
Key takeaways
Last Updated: Mar 27, 2024

Global Objects in JavaScript

Author GAZAL ARORA
0 upvote

Introduction: 

 

The global object in Javascript contains variables and functions that are accessible from anywhere. These are incorporated into the language or environment by default. 

However, other objects in the global scope are either created by the user script or provided by the host application.

 

The global object is known as the window object in a web browser; however, in Node.js, it is known as global. 

GlobalThis has recently been added to the language as a standardized name for a global object that should work in all situations. All major browsers are compatible with it.

Also See, Javascript hasOwnProperty

Getting Started:

 


Alert Ninjas! The ongoing emergence of new technologies in web development keeps the interest in this subject high. But, before you start on the big tasks, we recommend that you understand the fundamentals. With our JavaScript Course, you may jumpstart your web development career by mastering JS principles. It's now the cheapest it's ever been!

 


 

We'll use Windows in this article, presuming we're working in a browser. If your script has to run in multiple settings, you should use globalThis instead.

All of the global object's properties can be accessed directly:

window object in the Browser

 

Any global functions and variables declared with var (not let/const) can be accessed as properties of the window object.

 

Access Global Variables

var champ = "ninjas";
champ === window.champ; // Returns: true.

 

We can retrieve the value of a Global Variable champ from the window object by utilizing the variable name champ as a property name of the Global Object window.champ.

 

If a value is so important that you want it to be available globally, write it as a property:

 

// make constant user information global.

window.constUser = {
  name: "Leeza"
};

// somewhere else in code-
alert(constUser.name);  // Leeza

// Get it from window explicitly if we already have a local variable named "constUser"
alert(window.constUser.name); // Leeza

 

Access Global Functions

function welcome() {
  console.log("Hi, Ninjas");
}
window.welcome(); // It is the same as the normal invoking: welcome();

 

In the above example, we constructed a global Function named welcome and used the window object to call it. Try this code on an online JS compiler.

 

Using global variables, on the other hand, is often discouraged.  When a function receives "input" variables and generates a specific "output," the code is more readable, has fewer chances of errors, and is easier to test than when the function uses external or global variables.

Frequently Asked Questions

Q1: What is the difference between local objects and global objects?
Ans: The term "global object" refers to an object defined outside of all function bodies. Local object refers to the object specified within the body of a function. A local object is limited to the block in which it is declared.

Q2:   How do you access global objects?
Ans: The global object can be accessed using this operator in the global scope.

Q3: Is the ‘console’ a global object?
Ans: Yes, it is an inbuilt global object used to print to stdout and stderr. 

Key takeaways

Today, we learned about global objects. We learned:

  • The global object stores the variables that are available everywhere.
  • globalThis is the universal term for the global object. However, "window" for Browser and "global" for Node.js are more commonly used terms.
  • The global variables declared with var and global functions become properties of the global object in the Browser.
  • Only values that are global for our project should be stored in the global object. And always try to keep their count low.
     

Recommended Readings:

Want to build websites but don’t know where to start? 

Don’t worry; Coding Ninjas provides a whole course on NodeJS. It offers basic to Advance level understanding. Enroll today and build unique websites using Node. Click on the given link to enroll.

Live masterclass