The subscripts of an array can be thought of as properties of an object, and are referred to by their numeric index. Note that named properties added to an array cannot be indexed by number; they are separate from the array elements.
To create a new array, use the new operator and the Array() constructor, as in the following example.
var theMonths = new Array(12); theMonths = "Jan"; theMonths = "Feb"; theMonths = "Mar"; theMonths = "Apr"; theMonths = "May"; theMonths = "Jun"; theMonths = "Jul"; theMonths = "Aug"; theMonths = "Sep"; theMonths = "Oct"; theMonths = "Nov"; theMonths = "Dec";
var theMonths = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
var aString = "0123456789"; // This code sets aChunk to "456". var aChunk = aString.substring(4, 7); // This code sets anotherChunk to "456". var anotherChunk = aString.substring(7, 4); // This code sets the firstLetter variable to "J" // by using the array in the preceding array creation example. firstLetter = theMonths.substring(0,1);
Another property of the String object is the length property. This property contains the number of characters in the string (0 for an empty string). This a numeric value, and can be used directly in calculations.
The Math object has a number of predefined constants and functions. The constants are specific numbers. One of these specific numbers is the value of pi (approximately 3.14159...). This is the Math.PI constant, shown in the following example.
var radius = 5; var circleArea = Math.PI * radius * radius; // Note capitalization of Math and PI.
One of the built-in functions of the Math object is the exponentiation method, or Math.pow, which raises a number to a specified power. The following example uses both pi and exponentiation.
To create a new Date object, use the new operator, as shown in the following example.
/* 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 toDay variable. */ // Set variable to today's date. var toDay = new Date(); // Extract the year, the month, and the day. var thisYear = toDay.getFullYear(); var thisMonth = theMonths[toDay.getMonth()]; var thisDay = thisMonth + " " + toDay.getDate() + ", " + thisYear;
Largest possible number, about 1.79E+308; can be positive or negative. (Value varies slightly from system to system.)
Smallest possible number, about 5.00E-324; can be positive or negative. (Value varies slightly from system to system.)
Special nonnumeric value, "not a number."
Any positive value larger than the largest positive number (Number.MAX_VALUE) is automatically converted to this value; represented as infinity.
Any value more negative than the largest negative number (-Number.MAX_VALUE) is automatically converted to this value; represented as -infinity.
Number.NaN is a special constant that is defined as "not a number." An attempt to parse a string that cannot be parsed as a number returns Number.NaN. NaN compares unequal to any number and to itself. To test for a NaN result, do not compare against Number.NaN; use the isNaN() function instead.