This section describes the new features and changes for arrays.
In addition to creating an array using its constructor function, you can create it using literal notation.
arrayName = [element0, element1, ..., elementn]
arrayName is the name of the new array.
elementn is a list of values for the array's elements. When this form is specified, the array is initialized with the specified values as its elements, and the array's length is set to the number of arguments.
JavaScript initializes arrays created through literal notation, when the HTML page is loaded.
The following example creates the coffees array with three elements and a length of three.
coffees = ["French Roast", "Columbian", "Kona"]
Core method. Joins two arrays and returns a new array.
arrayName1.concat(arrayName2)
arrayName1 is the name of the first array.
arrayName2 is the name of the second array.
Array object
concat does not alter the original arrays, but returns a "one level deep" copy that contains copies of the same elements combined from the original arrays. Elements of the original arrays are copied into the new array as follows:
If a new element is added to either array, the other array is not affected.
Core method. Extracts a section of an array and returns a new array.
arrayName.slice(beginSlice,[endSlice])
arrayName is the name of an array.
beginSlice is the zero-based index at which to begin extraction.
endSlice is the zero-based index at which to end extraction.
Array object
slice does not alter the original array, but returns a new "one level deep" copy that contains copies of the elements sliced from the original array. Elements of the original array are copied into the new array as follows:
If a new element is added to either array, the other array is not affected.
In the following example slice creates a new array, newCar, from myCar. Both include a reference to the object myHonda. When the color of myHonda is changed to purple, both arrays are aware of the change.
<SCRIPT LANGUAGE="JavaScript1.2">
//Using slice, create newCar from myCar.
myHonda = {color:"red",wheels:4,engine:{cylinders:4,size:2.2}} myCar = [myHonda, 2, "cherry condition", "purchased 1997"] newCar = myCar.slice(0,2)
//Write the values of myCar, newCar, and the color of myHonda //referenced from both arrays. document.write("myCar = " + myCar + "<BR>") document.write("newCar = " + newCar + "<BR>") document.write("myCar[0].color = " + myCar[0].color + "<BR>") document.write("newCar[0].color = " + newCar[0].color + "<BR><BR>")
//Change the color of myHonda myHonda.color = "purple" document.write("The new color of my Honda is " + myHonda.color + "<BR><BR>")
//Write the color of myHonda referenced from both arrays. document.write("myCar[0].color = " + myCar[0].color + "<BR>") document.write("newCar[0].color = " + newCar[0].color + "<BR>")
</SCRIPT>
This writes:
myCar = [{color:"red", wheels:4, engine:{cylinders:4,
size:2.2}}, 2, "cherry condition", "purchased 1997"]
newCar = [{color:"red", wheels:4, engine:{cylinders:4,
size:2.2}}, 2]
myCar[0].color = red newCar[0].color = red
The new color of my Honda is purple
myCar[0].color = purple
newCar[0].color = purple
Core method. sort now works on all platforms. It no longer converts undefined elements to null, and it sorts them to the high end of the array. For example:
<SCRIPT> a = new Array(); a[0] = "Ant"; a[5] = "Zebra"; function writeArray(x) { for (i = 0; i < x.length; i++) { document.write(x[i]); if (i < x.length-1) document.write(", "); } } writeArray(a); a.sort(); document.write("<BR><BR>"); writeArray(a); </SCRIPT>
JavaScript in Navigator 3 prints:
ant, null, null, null, null, zebra
ant, null, null, null, null, zebra
JavaScript in Navigator 4 prints:
ant, undefined, undefined, undefined, undefined, zebra
ant, zebra, undefined, undefined, undefined, undefined
If you specify LANGUAGE="JavaScript1.2" in the script tag, using new Array(1) creates a new array with a[0]=1.
Without the LANGUAGE="JavaScript1.2" specification, new Array(1) sets the array's length to 1 and a[0] to undefined.
The following example prints 1. Without LANGUAGE="JavaScript1.2", it prints undefined.
<SCRIPT LANGUAGE="JavaScript1.2"> a=new Array(1); document.write(a[0] + "<BR>"); </SCRIPT>
When an array is the result of a match between a regular expression and a string, the array returns properties and elements that provide information about the match. An array is the return value of regexp.exec, string.match, and string.replace.
arrayName.propertyName arrayName[element0,..., elementn]
arrayName is the name of the array.
propertyName is one of the properties listed below.
elementn is one of the elements listed below.
To help explain the properties and elements, look at the following example and then refer to the table below:
<SCRIPT> //Match one d followed by one or more b's followed by one d //Remember matched b's and the following d //Ignore case
myRe=/d(b+)(d)/i; myArray = myRe.exec("cdbBdbsbz");
</SCRIPT>
The properties and elements returned from a match between a regular expression and a string are as follows (the examples are a result of the above script):
Property/ Element |
Description | Example |
input | A read-only property that reflects the original string against which the regular expression was matched. | cdbBdbsbz |
index | A read-only property that is the zero-based index of the match in the string. | 1 |
[0] | A read-only element that specifies the last matched characters. | dbBd |
[1], ...[n] | Read-only elements that specify the parenthesized substring matches, if included in the regular expression. The number of possible parenthesized substrings is unlimited. | [1]=bB [2]=d |
The returned array varies depending on the type of match that was executed. The following lists shows what is returned if the match is successful. (regexp is a regular expression, str is a string, and replaceText is the replacement text for the replace method.
regexp.exec(str) returns:
An array containing the match string, any parenthesized substring matches, and the input and index properties as described above.
str.match(regexp) returns:
An array containing the match string, the last parenthesized substring match (if included), and the input and index properties as described above.
str.match(regexp) where regexp includes the global flag (e.g. /abc/g) returns:
An array of all matches and the last parenthesized substring match (if included). input and index are undefined.
str.match(regexp) where regexp includes the global and ignore case flags (e.g. /abc/gi) returns:
An array of all matches. input and index are undefined.
str.replace(regexp, "replaceText") returns:
A string with the regular expression match replaced by replaceText. input and index are undefined.