Search within TutorialsPark

Javascript Global Object


Learn to use JavaScript Global object .


Javascript Global Object: Introduction

The JavaScript Global Object has some properties and methods which are globally defined and are available to all Variables of a JavaScript Program.

When the Javascript interpreter begins to parse the webpage, it creates a new global object and then assigns it a set of properties which defines:

  • Global Properties of undefined, Infinity and NaN
  • Global Functions like NaN(), parseInt() and eval()
  • Constructor Functions like Date(), RegExp(), String(), Object() and Array()
  • Global Objects like Math and JSON

For Client Side JavaScript, the Window object serves as the global object for all JavaScript code contained within the browser window.

This window object is referenced using window property instead of this to refer the global object.


Javascript Global Object:Safe URI-Encoding Methods

JavaScript Global object Method encodeURI() and encodeURIComponent() takes a Uniform Resource Identifier(URI), which contains characters which are not allowed within a URI standard, and then encodes it to fit the standard.

The URI encode method encodes the URI so that it is acceptable to the browser, by replacing all invalid characters with the special UTF-8 encoding .

The Difference is that, encodeURI() works on entire URL, while encodeURIComponent() works solely on a segment of URI (say any illegal values.)

Example: Javascript Global Object: URI Encoding Methods.

Give it a TRY! » Note: encodeURI() replaced only spaces with %20 while encodeURIComponent() encodes all nonalphanumeric character.


Javascript Global Object : Decode URIs

The counter to above methods are the, decodeURI() and decodeURIComponent() methods are available.

The method decodeURI() method decodes characters encoded by encodeURI(), while decodeURIComponent() decodes characters encoded by encodeURIComponent()

Example: JavaScript Global Object : Decode URIs Method

Give it a TRY! » Note: The URI methods replace the escape() and unescape() methods


Javascript Global Object : eval() Function

The JavaScript function eval() works like an interpreter , it accepts string as an argument, as soon as its done with evaluating the string it returns the result of execution.

Example: JavaScript Global Object : eval() Function

Give it a TRY! » Note: The eval() function evaluates the string value


Javascript Global Object : Window Object

The JavaScript Window Object is also a Global Object's delegate, hence all functions and variables declared in global scope become properties of the window object .

In the below demo, the global variable Slogan and the global Function shoutSlogan() are defined , the variable is accessed using window object , and then the function is called of the window object.

Example: JavaScript Global Object : Decode URIs Method

Give it a TRY! » Note: Window object is used to implement lot many things than just the global object.