SoFunction
Updated on 2025-04-06

Several methods related to Array object in JavaScript

push method
Adds a new element to an array and returns the new length value of the array.
([item1 [item2 [. . . [itemN ]]]])

parameter
arrayObj Required option. An Array object.
item, item2,. . itemN optional. The new element of the Array.

illustrate
The push method will add the new elements in the order in which they appear. If one of the parameters is an array, the array is added to the array as a single element. If you want to merge elements from two or more arrays, use the concat method.

Example
<script type="text/javascript">
var arrayObj = new Array(0,1,2,3,4);
(5,6,7,8,"Maple Rock","CnLei");
alert(arrayObj[-1]);
</script>

Require
Version 5.5

pop method
Removes the last element in the array and returns that element.
()
The required arrayObj reference is an Array object.

illustrate
If the array is empty, undefined will be returned.

Example
<script type="text/javascript">
var arrayObj = new Array(0,1,2,3,4);
alert(());
</script>

Require
Version 5.5

shift method
Removes the first element in the array and returns that element.
( )

parameter
The required arrayObj reference is an Array object.

illustrate
The shift method removes the first element in the array and returns that element.

Require
Version 5.5

unshift method
Insert the specified element into the array start position and return the array.
([item1[, item2 [, . . . [, itemN]]]])

parameter
arrayObj Required option. An Array object.
item1, item2,. . .,itemN optional. The element that will be inserted into the beginning of the Array.

illustrate
The unshift method inserts these elements into the beginning of an array, so these elements will appear in the array in the order in the sequence of parameters.

Require
Version 5.5

concat method (Array)
Returns a new array, which is composed of two or more arrays.
([item1[, item2[, . . . [, itemN]]]])

parameter
array1 Required option. The Array object to which all other arrays are to be joined.
item1,. . ., itemN optional. To connect to other items at the end of array1.

illustrate
The concat method returns an Array object containing the connection between array1 and any other items provided.

The items to be added (item1 … itemN) are added to the array in order from left to right. If an item is an array, add its contents to the end of array1. If the item is not an array, add it as a single array element to the end of the array.

The following is the copying element from the source array to the result array:

For object parameters copied from the array being connected to the new array, the copy still points to the same object. Regardless of which of the new array or the source array changes, it will cause the other to change.
For values ​​or strings connected to the new array, copy only its values. Changes in values ​​in one array do not affect the values ​​in another array.
Example
The following example illustrates the usage of the concat method when using an array:
function ConcatArrayDemo(){
 var a, b, c, d;
 a = new Array(1,2,3);
 b = "JScript";
 c = new Array(42, "VBScript);
 d = (b, c);
// Return array [1, 2, 3, "JScript", 42, "VBScript"]
 return(d);
}

Require
Version 3

Join method
Returns the string value, which contains all elements of the array connected together, separated by the specified delimiter.
(separator)

parameter
arrayObj Required option. Array object.
separator Required option. is a String object that serves as a separator between array elements in the final String object. If this parameter is omitted, the array elements are separated by a comma.

illustrate
If there is an element in the array that is not defined or is null, process it as an empty string.

Example
The following example illustrates the usage of the join method.
function JoinDemo(){
 var a, b;
 a = new Array(0,1,2,3,4);
 b = ("-");
 return(b);
}

Require
Version 2

sort method
Returns an Array object whose elements have been sorted.
(sortfunction)

parameter
arrayObj Required option. Any Array object.
sortFunction optional. is the name of the function used to determine the order of elements. If this parameter is omitted, the elements will be arranged in ascending order in ASCII character order.

illustrate
The sort method sorts the Array objects appropriately; no new Array objects are created during execution.

If a function is provided for the sortfunction parameter, the function must return one of the following values:
(1) Negative value, if the first parameter passed is smaller than the second parameter.
(2) Zero, if the two parameters are equal.
(3) Positive value, if the first parameter is larger than the second parameter.

Example
<script type="text/javascript">
function AscSort(x, y) {
  return x == y ? 0 : (x > y ? 1 : -1);
}

function DescSort(x, y) {
  return x == y ? 0 : (x > y ? -1 : 1);
}

function RandomSort(x, y) {
  return (() * 2 - 1 );
}

var array = [2,4,3,5,1,6,9,0,8];

("<p>Positive order: " + (AscSort) + "</p>");
("<p>Reverse order: " + (DescSort) + "</p>");
("<p>Random sort: " + (RandomSort) + "</p>");
("<p>Random sort: " + (RandomSort) + "</p>");
("<p>Random sort: " + (RandomSort) + "</p>");
</script>

Require
Version 2

slice method (Array)
Returns a segment of an array.
(start, [end])

parameter
arrayObj Required option. An Array object.
start Required option. The start element of the part specified in arrayObj is a subscript calculated from zero.
end optional. The end element of the part specified in arrayObj is a subscript calculated from zero.

illustrate
The slice method returns an Array object containing the specified part of arrayObj.

The slice method is copied all the way to the element specified by end, but does not include the element. If start is negative, handle it 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. If end is omitted, the slice method will be copied all the way to the end of arrayObj . If end appears before start, no elements are copied into the new array.

Example
In the following example, except for the last element, all elements in myArray are copied into newArray:
newArray = (0, -1)

splice method
Remove one or more elements from an array, and, if necessary, insert a new element at the removed element position, returning the removed element.
(start, deleteCount, [item1[, item2[, . . . [,itemN]]]])

parameter
arrayObj Required option. An Array object.
start Required option. Specifies the starting position for removing elements from the array, which is calculated from 0.
deleteCount is required. The number of elements to be removed.
item1, item2,. . .,itemN Required option. New element to be inserted at the location of the removed element.

illustrate
The splice method can remove the specified number of elements starting from the start position and insert new elements, thereby modifying arrayObj. The return value is a new Array object composed of the removed elements.

Require
Version 5.5

reverse method
Returns an Array object whose element order is reversed.
( )

parameter
arrayObj Required option, this parameter is an Array object.

illustrate
The reverse method inverts the position of an element in an Array object. During execution, this method does not create a new Array object.
If the array is discontinuous, the reverse method creates elements in the array to fill the intervals in the array. The values ​​of all elements created in this way are undefined.

Example
The following example illustrates the usage of the reverse method:
function ReverseDemo(){
var a, l; // Declare variables.
a = new Array(0,1,2,3,4); // Create an array and assign a value.
l = (); // Invert the content of the array.
return(l); // Return the result array.
}