- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is a Global Object in JavaScript?
Objects in JavaScript are different from the Global Object. Using the new operator, you cannot create global objects. It comes into existence when the scripting engine is initialized. After it is initialized, the functions and constants are available for use.
A global object allows you to do the following −
It gives access to built-in functions and values. Call alert directly like the below code snippet, with window −
alert("Demo Text"); // or window.alert("Demo Text");
It also gives access to global function declarations and var variables −
<html> <head> <script> var str = "Demo Text"; // using window alert( window.str ); </script> </head> <body> </body> </html>
The Global object in JavaScript is not like the simple objects created by the user. It is a completely different concept to initialize which we do not need to use the new keyword. It is an automatically initialising entity which is initialized with the initialisation of the scripting engine. The Global object is also represented using the window keyword. The window keyword denotes the global object which allow us to access and call any built-in function or value. To access and call any of the in-built methods and value in JavaScript you can use the below syntax.
NOTE − Please make a note of this line, that we can access and call any of the built-in functions or values even without using the window keyword. Because, by default the scripting engine assign the whole script with that window (The global object) object and we do not need to use window keyword again and again to access the functions and values. We can directly access them by their names.
Example
The below code example will help you understand that there is no difference between the calling of a function whether it is called using the window keyword or not −
<html> <body> <h2>Global Object in JavaScript</h2> <p> Enter any number: </p> <input type = "number" id = "inp1" /> <br> <p> Click the below button to see the results. </p> <p id = "result"> </p> <button onclick = "display()"> Click to See Results </button> <script> var result = document.getElementById("result"); function check() { var inp1 = document.getElementById('inp1'); var inpVal = Number(inp1.value); result.innerHTML += " The number <b> " + inpVal + " </b> is verified using <b> window.isNaN() </b> method and result is: <b> " + window.isNaN(inpVal) + " </b> <br> when the same number <b> " + inpVal + " </b> is verified using the <b> isNaN() </b> method, the result is: <b> " + isNaN(inpVal) + " </b><br> "; } function display() { check(); } </script> </body> </html>
In the above example, we are calling the same built-in method named isNaN() using the window keyword and by directly calling it to check whether the number entered by the user is of type number or not.
Let us see one more code example where we will call the user defined function or the function defined by us using the window keyword or the global object.
Algorithm − The algorithm of the above example and this example is almost similar. You just need to perform some changes by replacing the value of the type attribute of the input element to text and call the check() function using the window keyword inside the display() function.
Example
The below example will explain how we can call the user-defined functions using the global object −
<html> <body> <h2> Working with Global Object in JavaScript</h2> <p> Enter any number: </p> <input type = "text" id = "inp" /> <br> <p> Click the below button to invoke the check() method using the global object.</p> <p id = "result"> </p> <button onclick = "display()"> Click to See Results </button> <script> var result = document.getElementById("result"); function check() { var inp1 = document.getElementById('inp'); var inpVal = inp1.value; result.innerHTML += " The value you have entered is: " + inpVal + " <br> "; } function display() { window.check(); } </script> </body> </html>
In this example, we are invoking the check() method using the window keyword that means using the global object inside the display() function. The above example shows that we can either call or access the built-in of the user defined functions and values using the global object.
In this article, we have discussed about the global object in JavaScript. We have used two different code examples to understand the concept of the global object and to understand the similarity between the calling and accessing the functions and values using the window keyword and without using the window keyword, which is very important to understand before using the global object inside the code.