JavaScript has built-in global object, properties and methods

Global properties

Global properties include:

Global methods

Global Methods include:

Global objects

In this chapter we are going to review some of the global objects and methods, as well as all three properties.

URI encoding methods

The built-in URI encoding and decoding methods in JavaScript:

encodeURI()

This methods is used to encode the Uniform Resource Identifier - URI It encodes all the characters except these special characters  ; , / ? : @ & = + $.

The encodeURI cannot be used for HTTP GET and POST requests because special characters in GET and POST methods, “&”, “+” and “=” are not encoded.

decodeURI()

Decodes the URI encoded in encodeURI(). It decodes all the escape sequences encoded in encodeURI() except #. 

encodeURIComponent()

This function encodes all the special characters including ; , / ? : @ & = + $ which is not encoded in encodeURI(). It can be used for HTTP GET and POST methods.

decodeURIComponent()

This function decodes the URI component encoded in encodeURIComponent().

Example with URI encoding methods

eval() method

The eval() function executes the JavaScript expression or statements passed to it. We pass expression to eval() when we want to evaluate it at run-time.

Syntax

Object eval(String);

Object eval(Reader);

Object eval(String, Bindings);

Object eval(Reader, Bindings);

Object eval(String, ScriptContext);

Object eval(Reader, ScriptContext);

Example of eval() method

Below are described a few ways to use eval() method:

 

NOTE: The eval() method invokes the JavaScript interpreter when called. Hence calling it multiple times in a code takes a toll on speed of the program. It also poses security risk when malicious content is passed to the function as a string. Also it is better to use new function() or other alternatives than eval(), for better readability.

Global Object properties

JavaScript has following global object properties which can be used with all objects:

Infinity

This property represents numeric value of infinity. There is a positive infinity and a negative infinity. Infinity is displayed when the number exceeds the limit of storage.

( 1.797693134862315E+308 is upper limit to display positive infinity,                                                                 -1.797693134862316E+308 is lower limit to display negative infinity) .

By default Infinity is a positive infinity.

The behavior of Infinity :

Positive value(or POSITIVE_INFINITY) * POSITIVE_INFINITY = POSITIVE_INFINITY

Positive value(or POSITIVE_INFINITY) * NEGATIVE_INFINITY = NEGATIVE_INFINITY

 

Negative value(or NEGATIVE_INFINITY) * POSITIVE_INFINITY = NEGATIVE_INFINITY

Negative value(or NEGATIVE_INFINITY) * NEGATIVE_INFINITY = POSITIVE_INFINITY

 

0 * POSITIVE_INFINITY = NaN

0 * NEGATIVE_INFINITY = NaN

 

NaN * POSITIVE_INFINITY = NaN

NaN * NEGATIVE_INFINITY = NaN

 

POSITIVE_INFINITY / (negative value other than NEGATIVE_INFINITY)=

NEGATIVE_INFINITY

NEGATIVE_INFINITY / (negative value other than NEGATIVE_INFINITY)=

POSITIVE_INFINITY

 

 

POSITIVE_INFINITY / (positive value other than POSITIVE_INFINITY)=

                                                            POSITIVE_INFINITY

NEGATIVE_INFINITY / (positive value other than POSITIVE_INFINITY)=

                                                      NEGATIVE_INFINITY

 

POSITIVE_INFINITY / (POSITIVE_INFINITY or NEGATIVE_INFINITY) = NaN

NEGATIVE_INFINITY / (POSITIVE_INFINITY or NEGATIVE_INFINITY) = NaN

 

Number / POSITIVE_INFINITY = 0

Number / NEGATIVE_INFINITY = 0

Example of the global property Infinity

NaN

This global object property represents Not-a-Number. It cannot be written or configured. It is displayed when a Math function fails. To check if a number is NaN use isNaN() function. == and === does not work to test with NaN.

Example of the global property NaN

Undefined

Undefined global object property indicates if a variable is initialized / assigned a value or not. Undefined cannot be configured or written to a variable. It is JS primitive. If a value of a variable is not defined, the function returns undefined. Undefined is not a reserved word and can be used as variable.

Example of the global property Undefined

Window Object

It is the open window in browser. All browsers support this object and all JS global variables, objects, functions and document objects of HTML DOM are members of this object.

The <iframe> tags creates one window object for each frame other than the window object for the HTML document.

More about the Window Object can be read here.

 

›› go to examples ››