Function, Global, JSON, Math, Number,
String objects. The intrinsic objects have associated methods, functions, properties, and constants that are described in detail in the language reference.
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";
When you create an array using the
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", using // the lower value index as the starting index. 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.
var howLong = "Hello World".length // Sets the howLong variable to 11.
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;
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 calculate the volume of a sphere.
var volume = (4/3)*(Math.PI*Math.pow(radius,3));
Date object can be used to represent arbitrary dates and times, to get the current system date, and to calculate differences between dates. It has several properties and methods, all predefined. In general, the
To create a new
Date object, use the new operator, as shown in the following example.
var toDay = new Date(); var thisYear = toDay.getFullYear(); var thisMonth = theMonths[toDay.getMonth()]; var thisDay = thisMonth + " " + toDay.getDate() + ", " + thisYear;
In addition to the special numeric constants (
|Number.MAX_VALUE||Largest possible number, about 1.79E+308; can be positive or negative. (Value varies slightly from system to system.)|
|Number.MIN_VALUE||Smallest possible number, about 5.00E-324; can be positive or negative. (Value varies slightly from system to system.)|
|Number.NaN||Special nonnumeric value, "not a number."|
|Number.POSITIVE_INFINITY||Any positive value larger than the largest positive number (Number.MAX_VALUE) is automatically converted to this value; represented as infinity.|
|Number.NEGATIVE_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.