JavaScript 1.3 Overview, Part I: Top-Level Properties and Functions - Doc JavaScript | WebReference

JavaScript 1.3 Overview, Part I: Top-Level Properties and Functions - Doc JavaScript

Top-Level Properties and Functions

JavaScript 1.3 defines three properties of the global object, NaN, Infinity, and undefined.

NaN represents an IEEE floating point reserved for Not-A-Number value. In JavaScript 1.2, NaN was only supported as property of the Number object. In JavaScript 1.3, it is also defined as a property of the global object. The initial value of the property NaN is the IEEE's NaN. NaN is always unequal to any other numbers, including NaN itself. You can use the handy isNaN() method to check for a NaN value. A few of JavaScript methods (such as the Number constructor, parseFloat, and ParseInt) return NaN, if the value specified in the parameter is not a number. Go ahead and hit this button:. Netscape Navigator evaluates parseInt("A") and returns the NaN value.

The undefined value was not previously defined in JavaScript 1.2. undefined is also a property of the global object, whose value is undefined primitive value. A variable that does not have a value is equal to undefined primitive value. The following statement demonstrates the usage of undefined for checking if a variable has been assigned a value:

  var salary;
  if (salary == undefined) document.write("Your salary is not defined.");

In JavaScript1.2 we check if a variable has been assigned by just checking that the variable is true, which is less intuitive:

  var salary;
  if (salary) document.write("Your salary is not defined.");

Infinity is a primitive value that represents a positive infinite number value. In JavaScript 1.2, the infinity number was defined as two properties of the Number object: POSITIVE_INFINITY and NEGATIVE_INFINITY. In JavaScript 1.3, Infinity is defined as the property of the global object and is available as a top-level property. The value Infinity is greater than any other number including itself. Infinity behaves mathematically as it should. Anything multiplied by or added to Infinity is also Infinity. Anything divided by Infinity is 0. Hitting the following button will display the value of the Infinity property of the global object: .

isFinite() is a new global function. It is a top-level function of the global object. Use this function to determine if a number is a finite one. If the argument in isFinite(number) is NaN, positive infinity, or negative infinity, the function returns false. In all other cases, it returns true. For example, isFinite(parseInt("a")) returns false, while isFinite(parseInt(5)) returns true.

Created: September 14, 1998
Revised: September 14, 1998