FREE BOOK

Chapter 2: How to code a JavaScript application

Posted by Murach Free Book | Internet & Web November 02, 2009
This chapter presents a subset of JavaScript and DOM scripting that will soon have you writing significant applications. If you don't have any programming experience, this chapter also makes a great aptitude test. If you read it and can do the exercises at the end of the chapter, you're ready for the rest of this book.

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

Method Description
toFixed(digits) Returns a string that contains the value of the object rounded to the number of decimal places in the parameter.

Examples

var balance = 2384.55678; // creates a number object named balance
alert ( balance.toFixed(2) ); // displays 2384.56
// balance is still 2384.55678
balance = parseFloat( balance.toFixed(2) ); // balance is now 2384.56

Methods of the String object

Method Description
substr(start,length) Returns a new string that contains part of the original string. This substring starts at the position in the first parameter and contains the number of characters in the second parameter. The positions in a string start with zero.
toLowerCase() Returns a new string with the letters converted to lowercase.
toUpperCase() Returns a new string with the letters converted to uppercase.

Examples

var guest = "Ray Harris"; // creates a string object named employee
alert ( 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

Method Description
toDateString() Returns a string with the formatted date.
getMonth() Returns the month number from the date. The months are numbered starting with zero. January is 0 and December is 11.
getDate() Returns the day of the month from the date.
getFullYear() Returns the four-digit year from the date.

Examples

var today = new Date(); // creates a Date object named today
// assume the current date is 09/20/2008
alert ( today.toDateString() ); // displays Sat Sep 20 2008
alert ( today.getFullYear() ); // displays 2008
alert ( today.getMonth() ); // displays 8, not 9, for September

Description

  • When you declare a number or string variable, a Number or String object is created. Then, you can use the Number or String methods with the variable.
  • To create a Date object, assign new Date() to a variable. Then, you can use the Date methods with the variable.

Figure 2-12 How to use the Number, String, and Date objects

Total Pages : 20 1112131415

comments