How to use the Number, String, and Date objects
When you store a numeric or string value in a variable, it is automatically converted to the corresponding object type by JavaScript. This lets you use the properties and methods of the object without having to explicitly create the object. In figure 2-12, you can learn how to use some of the common methods of the Number, String, and Date objects.
For the Number object, only the toFixed method is listed. It rounds the numeric value of the object to the number of decimal places passed to it by the parameter and then converts the value to a string. As the first two statements in the examples show, using this method doesn't change the value of the variable. It just returns a rounded value as a string.
The third statement in this group of examples shows how you can chain the toFixed method and the parseFloat method to round a value to a fixed value and then convert it back to a numeric value. This numeric value is then assigned to the variable so it replaces the original value.
For the String object, just three methods are listed. The first lets you extract a substring from a string. The second and third methods let you convert a string to all lowercase or all uppercase. Here again, none of these methods modify the original value in the variable. If you want to change the original value, you need to assign the modified value to the variable as illustrated by the fourth statement in the examples.
Because there isn't a primitive data type for dates, you need to create a Date object before you can use its methods. When you create a new Date object, it is initialized with the date and time it was created. This is the date and time on the user's computer, not on the web server. If the date and time on the user's computer is wrong, the Date object will be initialized incorrectly.
Once a Date object has been created, you can use the methods in this figure to work with it. Here, the toDateString method converts the date to a string. And the getFullYear method extracts just the four-digit year from the date. In the last example, the getMonth method returns the month number. Note, however, that the month numbers start with 0, not 1.
In chapter 7, you'll learn that there are many other methods that you can use with Number, String, and Date objects. However, the methods in this figure should be all that you'll need for your first JavaScript applications.
One method of the Number object
Examples
var balance = 2384.55678; // creates a number object named balancealert ( balance.toFixed(2) ); // displays 2384.56// balance is still 2384.55678balance = parseFloat( balance.toFixed(2) ); // balance is now 2384.56
Methods of the String object
var guest = "Ray Harris"; // creates a string object named employeealert ( guest.toUpperCase() ); // displays "RAY HARRIS"alert ( guest.substr(0,3) ); // displays "Ray"guest = guest.substr(4,6); // guest is now "Harris"
Methods of the Date object
var today = new Date(); // creates a Date object named today// assume the current date is 09/20/2008alert ( today.toDateString() ); // displays Sat Sep 20 2008alert ( today.getFullYear() ); // displays 2008alert ( today.getMonth() ); // displays 8, not 9, for September
Description
Figure 2-12 How to use the Number, String, and Date objects