JavaScript Array Class



Navigation Aids -- This Page        Navigation Aids -- This Topic        Navigation Aids -- This Site








Quick Reference for Array Class



The Array Class Reference Table
Native class
Inherits From Object
Throws: RangeError
Constants Properties Methods Constructors
Syntax Arguments Returns
 
None length concat( )
join( )
pop( )
push( )
reverse( )
shift( )
slice( )
sort( )
splice( )
toLocalString( )
toString( )
unshift( )
new Array( )
new Array(size)
new Array(elements...)
size
elements
A new array.




Array Class Short Description

An Array is a collection of values grouped together under a single identifier. An array under JavaScript can contain arbitrary data types (unlike Java, where all elements must be of the same data type). The individual data values in an array are referred to as elements. The elements are reference by an index where the first element has an index of zero, the second element has an index of one and the last element has an index of (total elements - 1). This index value is said to have an offset of one from its relative position in the array. An array data type is of a reference type much like other object types. Where data values in an object type are addressed by a name identifier, the data values in an array are addressed by an index.





Array Class Constants

None





Array Class Instance Properties





Array Class Instance Methods

The Array class also inherits the methods of a super class, that is, the Object class. You can view the Object class methods here. The Array class contains methods with the same signature as the corresponding methods of the Object super class. These methods are toString() and toLocaleString(). These methods will override the corresponding methods of the super class.



The Methods for the Array Class
Method Syntax / Arguments Returns Description / Example
 
concat( ) Syntax:
array.concat(value,...)

Arguments: value, ...
Any number of values to be concatenated with array.
This method returns a new array. The new array will reflect the original array plus those values that were concatenated. A new array is created that is the result of the concatenating each of the arguments to the original array. The original array is not altered.
Example:
var aray = [a,b,c]; xray = aray.concat([x,y,z},[1,2,3]); // returns [a,b,c,x,y,z,1,2,3] to xray // aray is still [a,b,c]
join( ) Syntax:
array.join( )
array.join(separator)

Arguments: separator
The default separator is the comma. The separator is an optional string used to separate each element.
This method returns a string. The original array is converted to a string with all elements concatenated together. The separator string is inserted between each element. The join( ) method converts each of the elements of the original array into a string and concatenates those strings together. While joining the converted strings, the method also inserts the separator between each of the converted strings.
Example:
var aray = ["May","June","July"]; mys = aray.join(" / "); // returns "May / June / July" to mys // aray is still ["May","June","July"]
pop( ) Syntax:
array.pop( )

Arguments: None
This method returns the last element of the array. The pop( ) method behaves as follows:
  • deletes the last element of the array
  • decrements the array length value by one
  • returns the value of the element that was popped
If the array is empty (not initialized), pop( ) does not change the array or length value and returns an undefined value.
Example:
var aray = []; //aray empty aray = ["05","06","07"]; // initialize aray.pop(); //aray["05","06"]---returns "07" aray.push(7,8); //aray["05","06",7,8] aray.pop(); //aray["05","06",7]---returns 8 aray.pop(); //aray["05","06"]---returns 7 aray.pop(); //aray["05"]---returns "06" aray.pop(); //aray[ ]---returns "05" aray.pop(); //aray[ ]---returns undefined
push( ) Syntax:
array.push(value, ...)

Arguments: value, ...
One or more values to be appended to the end of an array.
The push( ) returns the new length of the array after it is appended. The push( ) method behaves as follows:
  • takes an argument list of one or more values
  • appends its arguments in order to the end of the array
  • increments the array length value by the number of arguments
  • returns the new length of the appended array
The array is modified directly. push() and pop() adhere to the FILO list method (First In Last Out).
Example:
var aray = ["05","06","07"]; // initialize aray.pop(); //remove last element aray.push(7,8); //aray["05","06",7,8] aray.push("09"); //aray["05","06",7,8,"09"] // length of aray started at 3 and ended at 5
reverse( ) Syntax:
array.reverse( )

Arguments: None
None The reverse( ) method behaves as follows:
  • reverses the order of the elements of an array
  • the specified array is updated in place
  • does not create a new array or return any value
Example:
var aray = ["05","06","07"]; // initialize // aray[0] equates to "05" // aray[1] equates to "06" // aray[2] equates to "07" aray.reverse( ) // reverse all elements // aray[0] equates to "07" // aray[1] equates to "06" // aray[2] equates to "05"
shift( ) Syntax:
array.shift( )

Arguments: None
shift( ) returns the element that was shifted out of the array. The shift( ) method behaves as follows:
  • shift() modifies the array directly, it does not create a new array
  • removes the first element of the array
  • returns the first element of the array
  • decrements the length property by one
  • subsequent elements are shifted down one position (left shift)
  • an empty array is unaltered and returns undefined
Example:
var aray = ["05","06","07"]; // initialize // aray[0] equates to "05" // aray[1] equates to "06" // aray[2] equates to "07" aray.shift( ) // shift out leftmost elements // aray[0] equates to "06" // aray[1] equates to "07" // aray[2] does not exist
slice( ) Syntax:
array.slice(start, end)

Arguments: start, end
start: the array index at where the slice is to begin.
end: the array index (not including) at where the slice is to end.
slice() returns a new array that represents the slice from the original array. The slice( ) method behaves as follows:
  • slice() does not modifies the original array directly
  • slice() returns a new array that contains the elements sliced from the original array
  • the returned array starts (first element) with the element specified with the start argument
  • the returned array includes all subsequent elements up to but not including the element specified by the end argument
  • the start and end arguments can be negative numbers
  • when the arguments are negative, the positions (start and/or end) are measured from the end of the originating array
  • when the end argument is not specified, the slice will include all elements from the starting position to the end of the originating array
Example:
var aray = ["01","02","03","04"]; aray.slice(0,1) // slice returns ["01"] aray.slice(1,3) // slice returns ["02","03"] aray.slice(2) // slice returns ["03","04"]
sort( ) Syntax:
array.sort( )
array.sort(comparef)

Arguments: comparef
Compare Function: an optional function that determines sorting order.
sort() returns a reference to the array. The sort( ) method behaves as follows:
  • sort() does modifies the original array directly
  • sort() will reorder the elements of the array in place
  • sort() has two modes, 1) with a compare function, 2) without
  • without a compare function:
    • all elements are converted to strings
    • all string elements are ordered by ASCII values
    • potential problems: "9" will sort higher than "456"
    • above, comparison begins with first character in string ("9" is greater than "4")
  • with a compare function:
    • a compare function can be built to control sort
    • the sort calls the compare function
    • the function expects two values passed
    • the sort passes two elements pair by pair to function
    • The compare function will return compare results:
      1. less than zero: value b should sort later than a
      2. zero: the order of a and b should not change
      3. greater than zero: value a should sort later than b
    • undefined values are not passed to the function and will be placed at the end of the array
  • Here is a simple function that is used in the following example: function sortcompare(a,b){      return a - b; }
Example:
var aray = [100,20,300,4]; aray.sort(sortcompare) // sort numerics // aray changed in place: [4,20,100,300]
splice( ) Syntax:
array.splice(startIndex, deletecount, value, ...)

Arguments:
startIndex: the element at which splice starts
deletecount: optional argument; number of elements to be spliced
Values,...: optional argument; values to be inserted
splice() returns the deleted elements (if any). The splice( ) method behaves as follows:
  • splice() does modifies the original array directly
  • splice() returns all deleted elements (if any)
  • the start argument is mandatory; the other arguments are optional
  • splice will delete all elements starting from and including the start element
  • if the optional deletecount argument is included, it specifies the element delete range (start element thru element at start+count)
  • if the optional deletecount argument is not included, splice deletes to the end of array
  • if the optional value argument(s) is included, the values replace the deleted values
  • the number of values in the parameter list need not be the same as the number of elements deleted
  • the length
  • splice will perform the following operations on an array:
    • delete only: arguments used (start and deletecount)
    • replace: arguments used (start, deletecount and replacement values)
    • insertion only: arguments used (start, deletecount=0, insertion values)
    • append: arguments used (start=array.length-1, deletecount=0, append values)
Example:
var aray = [1,2,3,4,5,6,7,8,9]; aray.splice(7) // aray becomes [1,2,3,4,5,6,7] // splice returns [8,9] aray.splice(1,3) // aray becomes [1,5,6,7] // splice returns [2,3,4] aray.splice(1,0,"a","b",0) // aray becomes [1,"a","b",0,5,6,7] // splice returns undefined
toLocalString( ) Syntax:
array.toLocalString( )

Arguments: None
Returns a localized string of an array. The toLocalString( ) method behaves as follows:
  • The original array remains unchanged.
  • returns a localized string that represents the array
  • converts each element to a string
  • concatenates all individual element strings and
  • uses a browser specific separator character
  • The toLocalString( ) method is the same as the toString() method except the later uses a comma to separate the elements that are concatenated together.

Example:
var aray = [1,2,3,4,5]; // initialize aray.toLocalString(); // returns: "1,2,3,4,5" // we used commas for separators; but depends on local settings
toString( ) Syntax:
array.toString( )

Arguments: None
Returns a string of an array. The toString( ) method behaves as follows:
  • The original array remains unchanged.
  • returns a string that represents the array
  • converts each element to a string
  • concatenates all individual element strings and
  • uses a comma as the separator character
  • The toString( ) method is the same as the toString() method except the later uses a comma to separate the elements that are concatenated together.

Example:
var aray = [1,2,3,4,5]; // initialize aray.toString(); // returns: "1,2,3,4,5" // commas are used for separators with toString method
unshift( ) Syntax:
array.unshift(value, ...)

Arguments: value, ...
One or more values to be inserted at the start of an array.
Returns the new length of the array. The unshift( ) method behaves as follows:
  • unshift() modifies the array directly, it does not create a new array
  • inserts the first argument into the first element of the array
  • all existing elements are shifted to higher indexes (right shift)
  • with multiple arguments; arg1 becomes element0, arg2 becomes element1 and so on
  • increments the length property by the number of argument values inserted
  • returns the new length of the array
Example:
var aray = ["05","06","07"]; // initialize // aray[0] equates to "05" // aray[1] equates to "06" // aray[2] equates to "07" aray.unshift("01","02") // shift into leftmost elements // aray[0] equates to "01" // aray[1] equates to "02" // aray[2] equates to "05" // aray[3] equates to "06" // aray[4] equates to "07"




Array Class Constructor





Array Class Constructor Example




Top            

Rx4AJAX        About Us | Topic Index | Contact Us | Privacy Policy | 2008 This Site Built By PPThompson