3.8. undefinedAnother special value used occasionally by JavaScript is the undefined value. undefined is returned when you use either a variable that has been declared but never had a value assigned to it or an object property that does not exist. Note that this special undefined value is not the same as null . Although null and the undefined value are distinct, the == equality operator considers them equal to one another. Consider the following: my.prop == null
This comparison is
TRue
if either the
my.prop
property does not exist or it does exist but contains the value
null
. Since both
null
and the
undefined
value
Unlike null , undefined is not a reserved word in JavaScript. The ECMAScript v3 standard specifies that there is always a global variable named undefined whose initial value is the undefined value. Thus, in a conforming implementation, you can treat undefined as a keyword, as long as you don't assign a value to the variable. If you are not sure that your implementation has the undefined variable, you can simply declare your own: var undefined; By declaring but not initializing the variable, you assure your implementation has the undefined value. The void operator (see Chapter 5) provides another way to obtain the undefined value. When the undefined value is used in a Boolean context, it converts to false . When used in a numeric context, it converts to NaN . And when used in a string context, it converts to "undefined". |
3.9. The Date ObjectThe previous sections have described all the fundamental datatypes supported by JavaScript. Date and time values are not one of these fundamental types, but JavaScript does provide a class of object that represents dates and times and can be used to manipulate this type of data. A Date object in JavaScript is created with the new operator and the Date( ) constructor (the new operator is introduced in Chapter 5, and you'll learn more about object creation in Chapter 7): var now = new Date( ); // Create an object holding the current date and time. // Create a Date object representing Christmas. // Note that months are zero-based, so December is month 11! var xmas = new Date(2006, 11, 25);
xmas.setFullYear(xmas.getFullYear( ) + 1); // Change the date to next Christmas.
var weekday = xmas.getDay( ); // It falls on a Tuesday in 2007.
document.write("Today is: " + now.toLocaleString( )); // Current date/time.
The Date object also defines functions (not methods; they are not invoked through a Date object) to convert a date specified in string or numeric form to an internal millisecond representation that is useful for some kinds of date arithmetic. You can find full documentation on the Date object and its methods in Part III. |
3.10. Regular Expressions
Regular expressions provide a rich and powerful syntax for describing textual patterns; they are used for pattern matching and for implementing search and replace operations. JavaScript has adopted the Perl programming language syntax for
Regular expressions are represented in JavaScript by the RegExp object and may be created using the
RegExp( )
constructor. Like the Date object, the RegExp object is not one of the fundamental datatypes of JavaScript; it is simply a specialized kind of object provided by all conforming JavaScript
Unlike the Date object, however, RegExp objects have a literal syntax and can be encoded directly into JavaScript programs. Text between a pair of
/^HTML/ /[1-9][0-9]*/ /\bjavascript\b/i
The
|