Home > Article > Web Front-end > What is the use of javascript push() method?
In JavaScript, the push() method can add one or more elements to the end of the array and return the new length; syntax "array.push(element 1, element 2, ..., element n )". The push() method directly modifies the array and changes the length of the array, rather than creating a new array.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
JavaScript push() method
push() method adds one or more elements to the end of the array and returns the new length .
Note: New elements will be added at the end of the array.
Note: This method changes the length of the array.
Syntax:
array.push(item1, item2, ..., itemN)
Parameters | Description |
---|---|
item1, item2, ..., itemN | Required. The element to add to the array. |
Return value: new length of array
Example:
<script type="text/javascript"> var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" document.write(arr + "<br />") document.write(arr.push("James") + "<br />") document.write(arr) </script>
Output:
George,John,Thomas 4 George,John,Thomas,James
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of What is the use of javascript push() method?. For more information, please follow other related articles on the PHP Chinese website!