There are two special data types available to test references to objects or the contents of variables. If a variable is currently of the type undefined, then it has not had any value stored in it. If it is a property, perhaps it has been deleted from the object.
A variable reference would also yield the undefined value if it has not been declared. However, a reference to an undeclared variable causes a run-time error.
Properties will yield the value null if they are supposed to contain an object and have been purposely nulled out. They will be undefined unless they have been set to null by an assignment.
You can examine variables and object properties with the enquiry function to determine the type of the value stored there. The enquiry functions are:
isNaN()
isFinite()
This operator behaves like an enquiry function:
typeof
Testing for null and undefined or comparing them one with another can be problematical and is certainly non-trivial. In older versions of the browsers, you can simulate missing values.
The null value can be simulated with this expression:
(void 0)
The undefined value can be simulated with this:
(void null)
Of course the simulations depend on the existence of some keywords that may or may not exist.
See also: | isFinite(), isNaN(), null, typeof, undefined type |
Prev | Home | Next |
Special number values | Up | Spiral() |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |