JavaScript Global Object



Navigation Aids -- This Page        Navigation Aids -- This Topic        Navigation Aids -- This Site








Quick Reference for Global Object



The Global Object Reference Table
Built-In Object
Properties Methods
 
infinity
nan
undefined
encodeURI( )
decodeURI( )
encodeURIComponent( )
decodeURIComponent( )
eval( )
isFinite( )
isNaN( )
parseFloat( )
parseInt( )
escape( )
unescape( )




Global Object Description

The Global object in JS is an object not a class. The Global object is one of two JavaScript Core built-in objects. The other built-in object is the Math object. The two built-in objects are a subset of the core native classes. However, the built-in objects behave very differently from their cousins, the native classes. Take this link if a comparison of the two types of core objects are of interest.

The Global object provides methods and properties that can be used anywhere from within your JavaScript code. In other words, the members of the Global object are always within scope. The Global object has no name (as with native classes). Therefore, a qualifying class name is not used to access global properties and methods. For example, the dot operator (access operator) is not used to access the parseFloat() method. All Global methods and properties have individual reference entries under their own name. For this reason, the global methods are some times referred to as built-in functions.

The Global object is created by the JavaScript interpreter once when the interpreter initially starts up. The interpreter initializes the Global object with the properties of many predefined constants and methods.

The Global object also acts as a catch-all for user-defined properties of a global context. Refer to our topic on variable scope.





Global Object Properties





Constructor Functions are Properties of the Global Object

The Global object defines the constructor functions that define all other native classes. The native class's constructor functions belong to the Global object and therefore, these constructor functions are given global scope. In other words, the Global object by containing the native class's constructor functions as it's properties, provide global access to all constructors of the native classes. The following list of constructors are properties of the Global object:





Global Object Methods


The Pre-defined Methods of the Global Object
Syntax / Arguments Returns / Throws Description / Example
 
Method:
encodeURI( )

Syntax:
encodeURI(uri)

Arguments:
uri: a string that contains a URI
Returns:
a copy of URI argument

Throws:
URIError
The encodeURI( ) method behaves as follows:
  • returns an encoded copy of its URI argument where certain characters are replaced by hexadecimal escape sequences
  • the following set of characters are not encoded
    • ASCII letters and digits
    • punctuation characters  ( ) ' * ~ ! . - _
    • URI protocol characters  ; / ? : # & = + $ , @
  • all other characters are encoded (a space would be encoded)
  • characters that are encoded use a hexadecimal escape sequence
  • the escape sequence is %xx, where the percent sign indicates the escape character and xx is the hexadecimal equivalent value of the ASCII character
  • this method will throw a URIError when the URI contains characters that can not be encoded
  • use decodeURI() to reverse the encoding applied by this method
  • use encodeURI() to insure the browser can use and understand your hyperlinks
  • the decodeURI() and encodeURI() methods should only be used on complete URIs
  • the decodeURIComponent() and encodeURIComponent() methods should only be used on URIs components (protocol, server, directory path, file, or fragment identifier)

Example: encoding is in red font
//here is a fake uri (note the space in filename) myuri = "http://www.asite.com/file 99.html#frag"; alert(encodeURI(myuri)); //encodeURI returns the URI complete with hex chars http://www.asite.com/file%2099.html#frag //encodeURI() has encoded the space in the filename: //the colon, slash and pound sign are not encoded
Method:
decodeURI( )

Syntax:
decodeURI(uri)

Arguments:
uri: a string that contains an encoded URI
Returns:
a copy of URI argument

Throws:
URIError
The decodeURI( ) method behaves as follows:
  • returns an decoded copy of its URI argument where certain hexadecimal escape sequences are replaced by the actual ASCII characters
  • the URI of the argument has previously been encoded with escape sequences such as %xx, where the percent sign indicates the escape character and xx is the hexadecimal equivalent value of the ASCII character
  • this method will throw a URIError when one or more of the escape sequences in the URI contains characters that can not be decoded
  • decodeURI() is used to reverse the encoding applied by encodeURI()
  • the decodeURI() and encodeURI() methods should only be used on complete URIs
  • the decodeURIComponent() and encodeURIComponent() methods should only be used on URIs components (protocol, server (hostname), directory path, file, or fragment identifier)

Example: encoding is in red font
//here is a fake uri (note the escape sequence in filename) myuri = "http://www.asite.com/file%2099.html#frag"; alert(decodeURI(myuri)); //decodeURI returns the URI without escape sequences http://www.asite.com/file 99.html#frag //decodeURI() has decoded the space in the filename:
Method:
encodeURIComponent( )

Syntax:
encodeURIComponent(string)

Arguments:
string: a string that contains a URI Component
Returns:
a copy of URI component

Throws:
URIError
The encodeURIComponent( ) method behaves as follows:
  • returns an encoded copy of its string argument (URI component) where certain characters are replaced by hexadecimal escape sequences
  • the following set of characters are not encoded
    • ASCII letters and digits
    • punctuation characters  ( ) ' * ~ ! . - _
  • the URI protocol characters are encoded  ; / ? : # & = + $ , @
  • all other characters are encoded (a space would be encoded)
  • characters that are encoded use a hexadecimal escape sequence
  • the escape sequence is %xx, where the percent sign indicates the escape character and xx is the hexadecimal equivalent value of the ASCII character
  • this method will throw a URIError when the string characters can not be encoded
  • use decodeURIComponent() to reverse the encoding applied by this method
  • the decodeURIComponent() and encodeURIComponent() methods should only be used on URIs components (protocol, server (host name), directory path, filename, or fragment identifier)

Example: encoding is in red font
//here is a fake uri (note the space in filename) myuri = "http://www.asite.com/file 99.html"; alert(encodeURIComponent(myuri)); //encodeURIComponent returns the URI complete with hex chars http%3A%2F%2Fwww.asite.com%2Ffile%2099.html //the http protocol chars plus the space are escaped //the period is not encoded
Method:
decodeURIComponent( )

Syntax:
decodeURIComponent(string)

Arguments:
string: a string that contains an encoded URI component
Returns:
a copy of the argument's URI component

Throws:
URIError
The decodeURIComponent( ) method behaves as follows:
  • returns a decoded copy of its URI component argument where certain hexadecimal escape sequences are replaced by the actual ASCII characters
  • the argument has previously been encoded with escape sequences such as %xx, where the percent sign indicates the escape character and xx is the hexadecimal equivalent value of the ASCII character
  • this method will throw a URIError when one or more of the escape sequences in the URI component contains characters that can not be decoded
  • decodeURIComponent() is used to reverse the encoding applied by encodeURIComponent()
  • the decodeURIComponent() and encodeURIComponent() methods should only be used on URIs components (protocol, server (host name), directory path, filename, or fragment identifier)

Example: encoding is in red font
//here is an encoded uri (escape sequences in red) myuri = "http%3A%2F%2Fwww.asite.com%2Ffile%2099.html"; alert(decodeURIComponent(myuri)); //decodeURIComponent returns the string without escape sequences http://www.asite.com/file 99.html
Method:
eval( )

Syntax:
eval(code)

Arguments:
code: a string that contains the expression to be evaluated
Returns:
the evaluated value

Throws:
SyntaxError
EvalError
The eval( ) method behaves as follows:
  • the code representing the method's argument may be:
    • a JavaScript expression
    • a JavaScript statement
    • a group of JavaScript statements
  • depending on the nature of the argument the eval() method will return:
    • an expression: the result of the expression
    • a statement: executes the statement and returns any result
    • multiple statements: executes the statements and returns the result from last statement
  • if the evaluation of the argument does not return a value, the method will return "undefined"
  • The eval() method is very slow and its use should be limited
  • this method will throw a EvalError when the method is not called properly
  • this method will throw a SyntaxError when the argument does not contain legitimate JavaScript code

Example:
var z; var astring = "5 + 6"; z = eval(astring); alert(z); //z becomes 11   eval("6 > 9");// returns false eval(6 > 9);// returns false; type conversion occurs eval("uu = `bad`");// returns bad eval(uu = "bad");// SyntaxError eval(y=8);// returns 8; y also is assigned 8
Method:
isFinite( )

Syntax:
isFinite(number)

Arguments:
number: the number to be tested
Returns:
true: if number is finite
false: if number is not finite

Throws:
SyntaxError
The isFinite( ) method behaves as follows:
  • returns true when number (argument) is finite
  • returns false when number is:
    • if number is NaN
    • if number is positive infinity
    • if number is negative infinity
  • this method will throw a SyntaxError when the argument does not evaluate to a legitimate numeric value

Example:
alert(isFinite(Infinity));// returns false alert(isFinite(-Infinity));// returns false alert(isFinite(NaN));// returns false alert(isFinite(0));// returns true alert(isFinite(987));// returns true alert(isFinite(-1));// returns true //type conversion occur for booleans alert(isFinite(true));// returns true alert(isFinite(false));// returns true alert(isFinite(null));// returns true alert(isFinite("abc"));// returns false alert(isFinite(abc));// SyntaxError //if abc is a declared variable but uninitialized: var abc; alert(isFinite(abc));// returns false //if abc is a declared and initialized variable: var abc = 8; alert(isFinite(abc));// returns true
Method:
isNaN( )

Syntax:
isNaN(arg)

Arguments:
arg: the value to be tested
Returns:
true: if number is NaN
false: if number is not NaN

Throws:
SyntaxError
The isNaN( ) method behaves as follows:
  • returns true when number (argument) is:
    • the argument is the value NaN
    • the argument is the value undefined
    • the argument is a string
    • when the argument is a variable that is declared but not initialized
  • returns false when number is not NaN (or any of the above)
  • this method will throw a SyntaxError when the argument does not evaluate to a legitimate numeric value
  • you can use this method to check for arithmetic errors, such as divide by zero

Example:
alert(isNaN(Infinity));// returns false alert(isNaN(-Infinity));// returns false alert(isNaN(NaN));// returns true alert(isNaN(0));// returns false alert(isNaN(987));// returns false alert(isNaN(-1));// returns false alert(isNaN(true));// returns false alert(isNaN(false));// returns false alert(isNaN(null));// returns false alert(isNaN(undefined));// returns true alert(isNaN("abc"));// returns true alert(isNaN(abc));// SyntaxError //if abc is a declared variable but uninitialized: var abc; alert(isNaN(abc));// returns true //if abc is a declared and initialized variable: var abc = 8; alert(isNaN(abc));// returns false alert(isNaN(0/0));// divide by zero returns true
Method:
parseFloat( )

Syntax:
parseFloat(string)

Arguments:
string: the string to be converted to a float
Returns:
number or
NaN: if string can not be converted

Throws:
SyntaxError
The parseFloat( ) method behaves as follows:
  • parses and returns the first number that occurs in string argument
  • parses stops when a value is found that is not a part of a number
  • parsing is from left to right
  • one decimal point in the string is OK; more than one stops parsing
  • when parsing stops for any of the above reasons, the method returns the parsed number up to the stopping point
  • returns NaN when argument does not begin with a valid number
  • parseFloat() will return a float only when a decimal point followed by numbers is encountered; an integer is returned for all other numbers; 7.00 returns the integer 7
  • this method will throw a SyntaxError when the argument does not evaluate to a legitimate numeric value

Example:
alert(parseFloat(Infinity));// returns Infinity alert(parseFloat(-Infinity));// returns -Infinity alert(parseFloat(NaN));// returns NaN alert(parseFloat(0));// returns 0 alert(parseFloat(987));// returns 987 alert(parseFloat(-1));// returns -1 alert(parseFloat(0.6));// returns .6 alert(parseFloat(7654.321));// returns 7654.321 alert(parseFloat(true));// returns NaN alert(parseFloat(false));// returns NaN alert(parseFloat(null));// returns NaN alert(parseFloat(undefined));// returns NaN alert(parseFloat("abc"));// returns NaN alert(parseFloat(abc));// SyntaxError alert(parseFloat(76.4.001));// SyntaxError alert(parseFloat("76.4.001"));// returns 76.4 alert(parseFloat(123t));// SyntaxError alert(parseFloat("123t"));// returns 123 //if abc is a declared variable but uninitialized: var abc; alert(parseFloat(abc));// returns NaN //if abc is a declared and initialized variable: var abc = 8; alert(parseFloat(abc));// returns 8 alert(parseFloat(0/0));// divide by zero returns NaN
Method:
parseInt( )

Syntax:
parseInt(string)
parseInt(string, radix)

Arguments:
string: the string to be converted to an integer
radix: the base of argument
Returns:
number or
NaN: if string can not be converted

Throws:
SyntaxError
The parseInt( ) method behaves as follows with regards to the parsing of the string argument:
  • parsing is from left to right
  • returns NaN when argument does not begin with a valid number
  • parses and returns the first number that occurs in string argument
  • parses stops when a value is found that is not a number
  • when parsing stops, the parsed portion of the string is returned as an integer
  • this method will throw a SyntaxError when the argument does not evaluate to a legitimate numeric value
The parseInt( ) method behaves as follows with regards to the radix argument:
  • the valid base numbers for the radix argument are 2 through 36
  • a radix less than 2 or greater than 36 returns a NaN
  • a review of the more common numbering schemes:
    Base System Range of
    Numbers
    Expression Result
    2 Binary 0 and 1 parseInt("011", 2) 3
    8 Octal 0 thru 7 parseInt("011", 8) 9
    10 Decimal 0 thru 9 parseInt("011", 10) 11
    16 Hex 0 thru 9
    A thru F
    parseInt("011", 16) 17
    16 Hex 0 thru 9
    A thru F
    parseInt("01F", 16) 31

Example:
alert(parseInt(Infinity));// returns NaN alert(parseInt(-Infinity));// returns NaN alert(parseInt(NaN));// returns NaN alert(parseInt(0));// returns 0 alert(parseInt(987));// returns 987 alert(parseInt(-1));// returns -1 alert(parseInt(0.6));// returns 0 alert(parseInt(7654.321));// returns 7654 alert(parseInt(true));// returns NaN alert(parseInt(false));// returns NaN alert(parseInt(null));// returns NaN alert(parseInt(undefined));// returns NaN alert(parseInt("abc"));// returns NaN alert(parseInt(abc));// SyntaxError alert(parseInt(76.4.001));// SyntaxError alert(parseInt("76.4.001"));// returns 76 alert(parseInt(123t));// SyntaxError alert(parseInt("123t"));// returns 123 //if abc is a declared variable but uninitialized: var abc; alert(parseInt(abc));// returns NaN //if abc is a declared and initialized variable: var abc = 8; alert(parseInt(abc));// returns 8 alert(parseInt(0/0));// NaN
Method:
escape( )

Syntax:
escape(string)

Arguments:
string: a string that is to be encoded
Returns:
a copy of the string encoded

Throws: N/A
The escape( ) method behaves as follows:
  • returns an encoded copy of its string argument where certain characters are replaced by hexadecimal escape sequences
  • the following set of characters are not encoded
    • ASCII letters and digits
    • punctuation characters  @ / * . - _
  • all other characters are encoded (a space would be encoded)
  • characters that are encoded use a hexadecimal escape sequence
  • the escape sequence is %xx, where the percent sign indicates the escape character and xx is the hexadecimal equivalent value of the ASCII character
  • use unescape() to reverse the encoding applied by this method
  • the escape() and unescape() methods were deprecated with ECMAScript v3 in favor of the encode() and decode() methods

Example: encoding is in red font
(escape("Hello World!");//returns:Hello%20World%21 (escape("!$&+=");//returns:%21%24%26%2B%3D (escape("@/*.-_");//returns:@/*.-_ (unescape("Hello%20World%21");//returns:Hello World!
Method:
unescape( )

Syntax:
unescape(string)

Arguments:
string: a string that is to be decoded
Returns:
a copy of the string decoded

Throws: N/A
The unescape( ) method behaves as follows:
  • use unescape() to reverse the encoding applied by escape() method
  • characters that were encoded have a hexadecimal escape sequence
  • the escape sequence is %xx, where the percent sign indicates the escape character and xx is the hexadecimal equivalent value of the ASCII character
  • the escape() and unescape() methods were deprecated with ECMAScript v3 in favor of the encode() and decode() methods

Example: encoding is in red font
(unescape("Hello%20World%21");//returns:Hello World!


Top            

Rx4AJAX        About Us | Topic Index | Contact Us | Privacy Policy | 2008 This Site Built By PPThompson