- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- 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
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>
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.
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 −
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.
The below example will explain how we can call the user-defined functions using the global object −
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.
Kickstart Your Career
Get certified by completing the courseGet Started