You uses the new keyword to create an array object.
To use as a function:
Array([param])
When the Array object is used as a function the return will be an array as well.
param:
The param argument is optional. If it is absent an empty array is returned.
If the param argument is present it can be one of the following:
A number indicating the size of the array you want to create.
A comma separated list of data values the array shall contain.
An example of using the new Array(param):
<script type="text/javascript">
var stringArr= new Array("Boolean","Math","Number","Object","String");
document.write("String array: "+stringArr+"<br>");
var arrObj= new Array(10);
document.write("Array with 10 undefined elements: "+arrObj+"<br>");
</script>
General information about the object member types:
Prototype created methods, properties or constants can only be used
on instances of an object or on a primitive datatype.
Constructor created methods, properties or constants can NOT be used
on instances of an object or on a primitive datatype.
When the concat method is called with zero or more arguments item1, item2, etc., it returns an array containing the array elements of the object followed by the array elements of each argument in order.
The elements of the array are converted to strings, and these strings are then concatenated, separated by occurrences of the separator.
If no separator is provided, a single comma is used as the separator.
Return value:
A string.
separator:
An optional separator string that is inserted between each of the array string-values.
An example of using the join() :
<script type="text/javascript">
var arr = ["Boolean","Math","Number","Object","String"];
// without a parameter streng - comma will be useddocument.write(arr.join()+"<br>");
// with a parameter streng document.write("<ul><li>"+arr.join("</li><li>")+"</li></ul>");
</script>
Syntax:
pop()
The last element of the array is removed from the array and returned.
Return value:
Last element value
An example of using the pop() :
<script type="text/javascript">
var myarray =[];
document.write("The array length: "+myarray.length+"<br>");
myarray.push("Hallo");
myarray.push(function(){alert("I am a function"); return"OK";});
document.write("The array length: "+myarray.length+"<br>");
document.write("The first element contains: "+myarray[0]+"<br>");
document.write("Execute second element with return: "+myarray[1]()+"<br>");
document.write("Remove the second element which contains: "
+myarray.pop()+"<br>");
document.write("The array length: "+myarray.length+"<br>");
document.write("Removes the remaining element that contains: "
+myarray.pop()+"<br>");
document.write("The array length: "+myarray.length+"<br>");
</script>
Syntax:
push([item1][,item2][,...]))
The arguments are appended to the end of the array, in the order in which they appear.
The new length of the array is returned as the result of the call.
Return value:
The new length of the array.
item(s):
One or more items to be appended to the array
An example of using the push() :
<script type="text/javascript">
var myarray =[];
document.write("The array length: "+myarray.length+"<br>");
myarray.push("Hallo");
myarray.push(function(){alert("I am a function"); return"OK";});
document.write("The array length: "+myarray.length+"<br>");
document.write("The first element contains: "+myarray[0]+"<br>");
document.write("Execute second element with return: "+myarray[1]()+"<br>");
document.write("Remove the second element which contains: "
+myarray.pop()+"<br>");
document.write("The array length: "+myarray.length+"<br>");
document.write("Removes the remaining element that contains: "
+myarray.pop()+"<br>");
document.write("The array length: "+myarray.length+"<br>");
</script>
Syntax:
reverse()
The elements of the array are re-arranged so as to reverse their order.
The object is returned as the result of the call.
Return value:
The re-arranged array.
An example of using the reverse() :
<script type="text/javascript">
var arr = ["Boolean","Math","Number","Object","String"];
// reverse the order of elements in the arrayvar rev=arr.reverse();
// The processed arraydocument.write(arr+"<br>");
// The returned array is the same as the processed arraydocument.write(rev+"<br>");
</script>
Syntax:
shift()
The first element of the array is removed from the array and returned.
Return value:
The first element of the array.
An example of using the shift() :
<script type="text/javascript">
var arr = ["Jan","Feb","Mar","Apr","May","Jun"];
var rem=arr.shift(); // Returns: Jan and arr=Feb,Mar,Apr,May,Jundocument.write("Returns: "+rem+" and arr="+arr+"<br>");
</script>
Syntax:
slice(start, end)
The slice method takes two arguments, start and end, and returns an array containing the elements of the array from element start up to, but not including, element end (or through the end of the array if end is undefined).
If start is negative, it is treated as (length+start) where length is the length of the array.
If end is negative, it is treated as (length+end) where length is the length of the array.
Return value:
A new array that contains the elements of array from the element specified by start, up to, but not including, the element specified by end.
start:
Index number of the start element to be included.
end:
Index number of the end element, but not to be included.
The elements of this array are sorted and returned.
If the comparefn is not specified the method will sort the array as if there are strings to be sorted. Data values ??in the array elements that are not strings will be temporarily converted to strings before sorting is performed.
For an ascending sort; comparefn function must return a positive value if the first parameter value is considered to be greater than the second parameter value and a negative value for the opposite.
For an descending sort; compare function must return a negative value if the first parameter value is considered to be greater than the second parameter value and a positve value for the opposite.
Return value:
The sorted array.
comparefn:
A function that accepts two arguments x and y and return either a value >0, 0, or <0
When called with two or more arguments start, deleteCount and (optionally) item1, item2,
etc., the deleteCount elements of the array starting at array index start are replaced by the arguments item1, item2.
Return value:
An array with the deleted items.
start:
Start index for the insertion and/or deletion of items.
deleteCount:
The number of elements, starting with and including start index, to be deleted from array.
item(s):
Zero or more Items to be inserted into the array, beginning at the index specified by start parameter.
An example of using the splice() :
<script type="text/javascript">
var arr = [21,52,63,74,85,96,17,28];
var rem=arr.splice(5); // Returns: 96,17,28 and arr=21,52,63,74,85document.write("Returns: "+rem+" and arr="+arr+"<br>");
rem=arr.splice(3,2); // Returns: 74,85 and arr=21,52,63document.write("Returns: "+rem+" and arr="+arr+"<br>");
rem=arr.splice(1,1); // Returns: 52 and arr=21,63document.write("Returns: "+rem+" and arr="+arr+"<br>");
rem=arr.splice(0,1,92,83); // Returns: 21 and arr=92,83,63document.write("Returns: "+rem+" and arr="+arr+"<br>");
</script>
Syntax:
toLocaleString()
The elements of the array are converted to strings using their toLocaleString methods, and these strings are then concatenated, separated by occurrences of a separator string that has been derived in an implementationdefined
locale-specific way.
The result of calling this function is intended to be analogous to the result of
toString, except that the result of this function is intended to be locale-specific.
Return value:
A string
Syntax:
toString()
The result of calling this function is the same as if the built-in join method were invoked for this object with no argument.
Return value:
A string
Syntax:
unshift([item1][, item2][,...])
The arguments are prepended to the start of the array, such that their order within the array is the same as the order in which they appear in the argument list.
item:
One or more items that specify the order.
An example of using the unshift() :
<script type="text/javascript">
var arr = ["Feb","Mar","Apr","May","Jun"];
var rem=arr.unshift("Jan"); // Returns: 6 and arr=Jan,Feb,Mar,Apr,May,Jundocument.write("Returns: "+rem+" and arr="+arr+"<br>");
</script>
The constructor property is a reference to the function that will be invoked to create a Array object.
Syntax:
Array.prototype
The prototype property is a reference to an object that contains all build-in methods that can be used by alle instances of Array-objects.
You can apply your own properties to the Array object through this prototype property.
Important note:
Is not directly available on an instantiated object.
An example of using the prototype :
<script type="text/javascript">
var arr =[3,4,5,62];
// summary is a property that has a value of a function
Array.prototype.summary=function(initial) {
var sum=initial;
for (var s=0 ; s<this.length; s++) {
sum+=this[s];
}
return sum;
}
// summary is a new method for all instances of arraysdocument.write("array sum: "+arr.summary(0)+"<br>");
arr =["I"," love"," my"," baby!"];
document.write("array sum: "+arr.summary("")+"<br>");
</script>