Microsoft® JScript™ Intrinsic Objects | JScript Tutorial Previous

> Microsoft JScript provides nine intrinsic (or "built-in") objects. They are the Array, Boolean, Date, b>Function, Global, Math, Number, Object, and String objects. Each of the intrinsic objects has associated methods and properties that are described in detail in the language reference. Certain of the objects are also described here.

##### Array Object

> In JScript, objects are handled as arrays and arrays are handled as objects. The subscripts of an array, which are entirely equivalent to the properties of an object, can be referred to by number (or by name, if you assign names to them). To create a new array, use the new operator and the Array() constructor, as in the following example.

> >     var theMonths = new Array(12)  {
> >     theMonths[0] = "Jan";
> >     theMonths[1] = "Feb";
> >     theMonths[2] = "Mar";
> >     theMonths[3] = "Apr";
> >     theMonths[4] = "May";
> >     theMonths[5] = "Jun";
> >     theMonths[6] = "Jul";
> >     theMonths[7] = "Aug";
> >     theMonths[8] = "Sep";
> >     theMonths[9] = "Oct";
> >     theMonths[10] = "Nov";
> >     theMonths[11] = "Dec";
> >     }

> When you create an array by using the Array keyword, JScript includes in the array a write-only length property, which records the number of entries in the array. If you do not specify a number, the length is set to 0, and the array has no entries. If you specify a number, the length is set to that number. If you specify more than one parameter, the parameters are used as entries in the array, and the number of parameters is assigned to the length property, as in the following example, which is equivalent to the preceding one.

> >     var theMonths = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun",
> >     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

> JScript automatically changes the value of length if you add elements to an array that you created with the Array keyword.

##### String Object

> In JScript, strings are objects. This means that any time you declare a string variable or use a string literal, what you're actually doing is creating a new string object. The String object has certain built-in methods, which you can use with your strings. One of these is the substring method, which returns part of the string. It takes two numbers as its arguments.

> >     aString = "0123456789";
> >     var aChunk = aString.substring(4, 7);  // Sets aChunk to "456".
> >     var aNotherChunk = aString.substring(7, 4);  // Sets aNotherChunk to "456".

> >     // Using the preceding Array creation example:
> >     firstLetter = theMonths [5].substring(0,1);  // Sets the firstLetter variable to "J".

> Another property of the String object is the length property. This property contains the number of characters in the string, which is 0 for an empty string. This a numeric value, and can be used directly in calculations.

> >     var howLong = "Hello World".length  // Sets the howLong variable to 11.

##### Math Object

> The Math object has a number of properties and methods, all predefined. The properties are specific numbers. One of these is the value of pi (approximately 3.14159...). This is the Math.PI property, shown in the following example.

> >     // A radius variable is declared and assigned a numeric value.
> >     var circleArea = Math.PI * radius * radius;  // Note capitalization of Math and PI.

> One of the built-in methods of the Math object is the exponentiation method, or pow, which raises a number to a specified power. The following example makes use of both pi and exponentiation.

> >     // This formula calculates the volume of a sphere with the given radius.
> >     volume = (4/3)*(Math.PI*Math.pow(radius,3));

##### Date Object

> Use the Date object to capture today's date, and to calculate differences between dates. It has a number of properties and methods, all predefined. In general, the Date object provides the day of the week; the month, day, and year; and the time in hours, minutes, and seconds. This information is based on the number of milliseconds since January 1, 1970, 00:00:00.000 GMT. GMT stands for "Greenwich Mean Time"; the preferred term is UTC, or "Universal Coordinated Time," which refers to signals issued by the World Time Standard.

> Note As far as JScript is concerned, time begins at midnight on January 1, 1970; you cannot ask JScript to create a Date object that represents an earlier time than that. If you need to deal with earlier times you must write your own code to do so, a formidable task.

> To create a new Date object you use the new operator. The following example calculates, for the current year, the number of days that have passed and the number of days that are left.

> >     /*
> >     This example uses the array of month names defined previously.
> >     The first statement assigns today's date, in "Day Month Date 00:00:00 Year"
> >     format, to the thisIsToday variable.
> >     */
> >     var thisIsToday = new Date();

> >     var toDay = new Date();  // Capture today's date.

> >     // Extract the year, the month, and the day.
> >     var thisYear = toDay.getYear() + 1900;
> >     var thisMonth = theMonths[toDay.getMonth()];
> >     var thisDay = thisMonth  + " " + toDay.getDate() + "," + (parseInt(toDay.getYear()) + 1900);

> >     // Determine the # of days since the start.
> >     thisDay = Math.round(Date.parse(thisDay)/8.64e7);

> >     // Do the same for the beginning of the year.
> >     var firstDay = "Jan 1, " + thisYear;
> >     firstDay = Math.floor(Date.parse(firstDay)/8.64e7);

> >     // Do it again for the end of the year, in case it's a leap year.
> >     var lastDay = "Dec 31, " + thisYear;
> >     lastDay = Math.floor(Date.parse(lastDay)/8.64e7);

> >     // Compute the number of days in the year.
> >     var daysInYear = (lastDay - firstDay) + 1;

> >     // Determine how many days have elapsed, and how many are left.
> >     var daysElapsed = thisDay - firstDay;
> >     var daysLeft = daysInYear - daysElapsed;

> >     // Set up comments for most of the year.
> >     var comment1 = daysElapsed+ " days have elapsed in the year.";
> >     var comment2 = "That means there are " + daysLeft + " days left in " + thisYear + ".";

> >     // Cover the special cases: beginning & end of year, and single day.
> >     if (daysElapsed == 0)  {
> >     comment1 = "It's January first, " + thisYear + ".";
> >     }
> >     if (daysElapsed == 1) {
> >     comment1 = "Only one day gone so far.";
> >     }
> >     if(daysElapsed == daysInYear) {
> >     comment1 = thisYear + " is just about over.";
> >     }

> >     if (daysLeft == 0)  {
> >     comment2 = "Best wishes for the New Year!";
> >     }
> >     if (daysLeft == 1)  {
> >     comment2 = "There's only one day left in " + thisYear + ".";
> >     }
> >     if (daysLeft == daysInYear)  {
> >     comment2 = "Happy New Year!";
> >     }

##### Number Object

> In addition to the special numeric properties (PI, for example) that are available in the Math object, several other properties are available in Microsoft JScript through the Number object.

> Property | Description
> MAX_VALUE | Largest possible number, about 1.79E+308; can be positive or negative. (Value varies slightly from system to system.)
> MIN_VALUE | Smallest possible number, about 2.22E-308; can be positive or negative. (Value varies slightly from system to system.)
> NaN | Special nonnumeric value, "not a number."
> POSITIVE_INFINITY | Any positive value larger than Number.MAX_VALUE is automatically converted to this value; represented as "Inf".
> NEGATIVE_INFINITY | Any negative value larger than -Number.MAX_VALUE is automatically converted to this value; represented as "-Inf".

> Number.NaN is a special property that is defined as "not a number." Division by zero, for example, returns NaN. An attempt to parse a string that cannot be parsed as a number also returns Number.NaN. NaN compares unequal to any number and also to itself. To test for a NaN result, do not compare against Number.NaN; use the isNaN() function instead.

© 1998 Microsoft Corporation. All rights reserved. Terms of Use.