JScript Language Elements

| Tutorial | Reference |

Built-in Objects: String, Math, Date.

| Objects | Built-In Objects | Creating Your Own Objects |

Several kinds of objects are built into JScript. As has already been mentioned, objects can be handled as arrays. Arrays, can be handled as objects, and you can create them at will. An Array() function is provided for the purpose of creating new array objects.

EXAMPLE:
var monthName = new Array(12)  {
monthName[0] = "Jan";
monthName[1] = "Feb";
monthName[2] = "Mar";
monthName[3] = "Apr";
monthName[4] = "May";
monthName[5] = "Jun";
monthName[6] = "Jul";
monthName[7] = "Aug";
monthName[8] = "Sep";
monthName[9] = "Oct";
monthName[10] = "Nov";
monthName[11] = "Dec";
}
Strings
In JScript, strings are handled as objects. This means that any time you declare a string variable, 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 string variables. (LINK to reference? Include this info directly?) One of these is the substring method, which returns part of the string. It takes two numbers as its arguments. Under ordinary circumstances, the first index is smaller than the second, and the substring method returns the characters starting at the first index and ending just before the second. If the first index is larger, the result is nonetheless the same. If the two indices are equal, the method returns the empty string.

EXAMPLES:
aString = "0123456789";
var aChunk = aString.substring(4, 7);    // Sets aChunk to "456". Note, substring goes from the first
                                         //  index to one less than the second index.
var aNotherChunk = aString.substring(7, 4);        //  Sets aNotherChunk to "456".

// Harking back to the Array creation example above:
aChunk = monthNames[5].substring(0,1);        //  Sets aChunk to "J".
The Reference section contains more complete information on the String object.
The Math Object:
The Math object is built into JScript. It has a number of methods and a number of properties, all of which are predefined. You can use these when you need them. One of the built-in properties is the value of pi (approximately 3.14159265358979...). This is the Math.PI property. (Note capitalization, which is significant here!)

One of the built-in methods is the exponentiation method, Math.pow(num1,num2), which raises a number (the first argument, num1) to a specified power (the second argument, num2).

EXAMPLE:
//  {JScript statements in which a radius variable is declared and assigned a numeric value}
circumf = Math.PI * 2 * radius;        //  Calculates the circumference of a circle.
area = Math.PI * radius * radius;        //  Calculates the area of the circle
volume = (4/3)*(Math.PI*Math.pow(radius,3));        //  Calculates the volume of a sphere
//  with the same radius
The Date Object:
The built-in Date object lets you determine the date, and also lets you calculate differences between dates. It has a number of properties and methods. 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 actually based on the number of milliseconds since the epoch, which is set to 00:00:00.000 GMT ("Greenwich Mean Time", an now obsolete term), on January 1, 1970.

The Date object has methods for converting GMT (or UTC, which stands for "Coordinated Universal Time") to correct time for other timezones, and vice versa. It also has various other useful methods.

NOTE: As far as JScript is concerned, time begins at the epoch. In other words, you cannot ask JScript to create a Date object that is earlier than the epoch. If you need one, you must write code to construct it.

To create a new Date object, use the new statement.

EXAMPLES:
var thisDayInHistory = new Date();
document.write(thisDayInHistory);        //  Writes today in "Day Month Date 00:00:00 Year" format.


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

var toDay = new Date();
var thisMonth = monthName[toDay.getMonth()];
var thisDay = thisMonth  + " " + toDay.getDate() + "," + (parseInt(toDay.getYear()) + 1900);
thisDay = Math.floor(Date.parse(thisDay)/8.64e7);

var firstDay = "Jan 1, 1996";
firstDay = Math.floor(Date.parse(firstDay)/8.64e7);
toDay = thisDay - firstDay;
document.write(toDay + " days have elapsed in the year 1996.<br>");

var daysLeft = 365 - toDay;
document.write("That means there are only " + daysLeft + " days left.<br>");



© 1996 by Microsoft Corporation.