JavaScript Editor JavaScript Debugger     JavaScript Editor 

Team LiB
Previous Section Next Section


The Global object is a seldom-mentioned object that is a catchall for top-level properties and methods that are not part of any other object. You cannot create an instance of the Global object; it is defined in the ECMA-262 standard to be a place for globally accessible, otherwise homeless properties to reside. It provides several essential properties that can be used anywhere in JavaScript. Table 7-2 summarizes its most useful methods. These methods are called directly and are not prefixed with “global.” In fact, doing so will result in an error. It is because the methods appear unrelated to any particular object that some documentation on JavaScript refers to these as “global” or built-in functions.

Table 7-2: Globally Available Methods





>Takes a string and returns a string where all non-alphanumeric characters such as spaces, tabs, and special characters have been replaced with their hexadecimal equivalents in the form %xx.

>var aString="O'Neill & Sons";
// aString = "O'Neill & Sons"
aString = escape(aString);
// aString="O%27Neill%20%26%20Sons"


>Takes a string and executes it as JavaScript code.

>var x;
var aString = "5+9";
x = aString;
// x contains the string "5+9"
x = eval(aString);
// x will contain the number 14


>Returns a Boolean indicating whether its number argument is finite.

>var x;
x = isFinite('56');
// x is true
x = isFinite(Infinity)
// x is false


>Returns a Boolean indicating whether its number argument is NaN.

>var x;
x = isNaN('56');
// x is False
x = isNaN(0/0)
// x is true
x = isNaN(NaN);
// x is true


>Converts the string argument to a floating-point number and returns the value. If the string cannot be converted, it returns NaN. The method should handle strings starting with numbers and peel off what it needs, but other mixed strings will not be converted.

>var x;
x = parseFloat("33.01568");
// x is 33.01568
x = parseFloat("47.6k-red-dog");
// x is 47.6
x = parseFloat("a567.34");
// x is NaN
x = parseFloat("won't work");
// x is NaN


>Converts the string argument to an integer and returns the value. If the string cannot be converted, it returns NaN. Like parseFloat(), this method should handle strings starting with numbers and peel off what it needs, but other mixed strings will not be converted.

>var x;
x = parseInt("-53");
// x is -53

x = parseInt("33.01568");
// x is 33

x = parseInt("47.6k-red-dog");
// x is 47

x = parseInt("a567.34");
// x is NaN

x = parseInt("won't work");
// x is NaN


>Takes a hexadecimal string value containing some characters of the form %xx and returns the ISO-Latin-1 ASCII equivalent of the passed values.

>Var aString="O%27Neill%20%26%20Sons";
aString = unescape(aString);
// aString = "O'Neill & Sons"
aString = unescape("%64%56%26%23");
// aString = "dV&#"

The Global object also defines the constants NaN and Infinity that were used in the examples in Table 7-2. However, similar constants are also provided by the Number object discussed later in the chapter.

The Global methods are very useful and will be used in examples throughout the book. Aspiring JavaScript programmers should try to become very familiar with them. The eval() method in particular is quite powerful and it is interesting to see how very succinct scripts can be written with it. However, with this power comes a price and many scripts using eval() produce very tricky runtime problems, so proceed with caution.

Another interesting consideration for Global methods is the escaping of strings provided by escape() and unescape(). Primarily, we see this done on the Web in order to create URL safe strings. You probably have seen this when working with forms. While these methods would be extremely useful, the ECMAScript specification suggests that escape() and unescape() are deprecated in favor of the more aptly named encodeURI(), encodeURIComponent(), decodeURI(), and decodeURIComponent(). Their use is illustrated here:

var aURLFragment = encodeURIComponent("term=O''Neill & Sons");document.writeln("Encoded URI Component: "+aURLFragment);document.writeln("Decoded URI Component: "+decodeURIComponent(aURLFragment));

var aURL = encodeURI("''Neill & Sons");
document.writeln("Encoded URI: "+ aURL);
document.writeln("Decoded URI: "+ decodeURI(aURL));

While these methods are part of the specification, programmers still often avoid them given that some browsers do not support them. Furthermore, for better or worse, escape() and unescape() are commonly used by current JavaScript programmers so their usage doesn’t seem to be dying down in favor of the specification functions any time soon. A complete documentation of Global, including these issues, can be found in Appendix B.

Team LiB
Previous Section Next Section

JavaScript Editor JavaScript Debugger     JavaScript Editor