Global properties and methods in JavaScript.

Static members

Some of the global object have static properties and methods that are accessed directly from the prototype as opposed to an instance. Since there are only a few, I will list them here:

Date.now(); //Mozilla only
Date.parse(dateString);
Date.UTC(year, month[, date[, hour[, min[, sec[, ms]]]]]);
Math.PI; //etc, etc, because all members of Math are static!
Number.MAX_VALUE;
Number.MIN_VALUE;
Number.NaN;
Number.NEGATIVE_INFINITY;
Number.POSITIVE_INFINITY;
String.fromCharCode(num1, ..., numN);

Global properties

JS has 3 top-level properties that are always available.

Infinity

Implemented: JS 1.3; ECMA-262.
A numeric value representing infinity. Initially equivalent to Number.POSITIVE_INFINITY, but there is also Number.NEGATIVE_INFINITY.

NaN

Implemented: JS 1.3; ECMA-262.
A value representing Not-A-Number. Initially equivalent to Number.NaN. In pseudo-code: NaN <> NaN <> Number.NaN, so use isNaN() for comparison instead.

undefined

Implemented: JS 1.0; ECMA-262.
A variable that has not been assigned a value is of type undefined. EG:

var x;
alert(x === undefined); //alerts true
alert(typeof x == 'undefined'); //alerts true

Global functions

These are global functions that are built into JavaScript and are not associated with any objects. The constructors of global objects (EG: String('hi');) are effectively global functions too. Some of the operators are effectively functions as well.

encode or escape related

These functions are most often used for data going to or from an URL, URI, query string, etc.

decodeURI(encodedURI)

Implemented: JS 1.5.
Undoes encodeURI().

decodeURIComponent(encodedURIComponent)

Implemented: JS 1.5.
Undoes encodeURIComponent().

encodeURI(URI)

Implemented: JS 1.5.
Returns a string encoding all characters (including spaces) except for the following:

Characters are encoded in UTF-8 with 1-4 sequences of %XX where X is a hexadecimal digit.

encodeURIComponent(URIComponent)

Implemented: JS 1.5.
Like encodeURI() except that the parameter is a URI component (like user entered data), so it also encodes the URI reserved characters. EG:

encodeURI('http://fake.com?x='+'Thyme &time=again'); 
//http://fake.com?x=Thyme%20&time=again
encodeURIComponent('http://fake.com?x='+'Thyme &time=again'); 
//http%3A%2F%2Ffake.com%3Fx%3DThyme%20%26time%3Dagain
escape('http://fake.com?x='+'Thyme &time=again'); 
//http%3A//fake.com%3Fx%3DThyme%20%26time%3Dagain

escape(URI)

Implemented: JS 1.0; JS 1.5 deprecated.
Returns a string encoding all characters (including spaces) except for:

Characters are encoded using 2 digit IETF escapes: %XX where X is a hexadecimal digit. This implies that it only handles a subset of Unicode characters. This is roughly equivalent to the Server.URLEncode() method of ASP, and the Escape() function of VBS.

So the problem with escape() is three-fold: An incomplete set of URI reserved characters, it doesn't escape all Unicode characters, and it doesn't distinguish between a URI component and a whole URI..

unescape(escapedURI)

Implemented: JS 1.0; JS 1.5 deprecated.
Undoes escape(). Similar to the VBS Unescape() function. ASP does not have an equivalent method.

Miscellany

eval(string[, object])

Implemented: JS 1.0; ECMA-262 except for the 2nd parameter
Takes a string and evaluates it as a JS expression, i.e. without reference to an object. If the optional 2nd parameter is provided, then the evaluation is restricted to the passed object. As of 2008, the 2nd parameter is only available in Mozilla. eval() is a potentially dangerous function and should be used cautiously and rarely. EGs:

str = "3 * x + 4";
x = 2;
myValue = eval(str);
//myValue is equal to 10. You could have done any number of things to get the value of 10.

eval(new String("2 + 2")); //returns a string "2 + 2"
eval("2 + 2");             //returns 4

isFinite(number)

Implemented: JS 1.3; ECMA-262.
Checks if a value is a finite number. If the number is NaN, positive infinity or negative infinity, then this method returns false, otherwise it returns true.

isNaN(value)

Implemented: JS 1.0; ECMA-262.
Checks if value is NaN. EG:

isNaN(NaN)      //returns true
isNaN("string") //returns true
isNaN("12")     //returns false
isNaN(12)       //returns false

parseFloat(string)

Implemented: JS 1.0; ECMA-262.
Converts string to floating-point number. JS 1.0+.

parseInt(string[,radix])

Implemented: JS 1.0; ECMA-262.
Converts string to integer of an optionally specified radix or base. If the input string begins with "0x", the radix is 16 (hexadecimal). If the input string begins with "0", the radix is 8 (octal). [This octal feature is deprecated in JS 1.5.] If the input string begins with any other value, the radix is 10 (decimal). Leading and trailing spaces are allowed. Non-numeric characters are allowed after the number part. If the string has multiple numbers, it returns the first number. EG:

parseInt("10"); // returns 10
parseInt("10.23"); // returns 10
parseInt("34 45 66"); // returns 34
parseInt(" 60 "); // returns 60
parseInt("50 years"); // returns 50
parseInt("He was 78"); // retnurns NaN

"<br>");
parseInt("10", 10); // returns 10
parseInt("010"); // returns 8
parseInt("10", 8); // returns 8
parseInt("0x10"); // returns 16
parseInt("10", 16); // returns 16

taint()

Implemented: JS 1.0; JS 1.4 deprecated.
Taints or marks a data element or script.

untaint()

Implemented: JS 1.0; JS 1.4 deprecated.
Removes tainting from a data element or script.

Page Modified: (Hand noted: ) (Auto noted: )