This article brings you relevant knowledge about javascript, which mainly introduces the use of JavaScript’s push(), pop(), unshift(), and shift() methods, as follows Let's take a look, I hope it will be helpful to everyone.
[Related recommendations: javascript video tutorial, web front-end]
pop
(): This method is used to delete the last element of the array and return the deleted element.
Official syntax:
arrayObject.pop()
shift
(): This method is used to delete the first element of the array and return the deleted element.
Official syntax:
arrayObject.shift()
push
(): This method is used to add one or more elements to the end of the array and return the new length.
Official syntax:
arrayObject.push(newelement1,newelement2,....,newelementX)
unshift
(): This method is used to add one or more elements to the beginning of the array and return the new length.
Official syntax:
arrayObject.unshift(newelement1,newelement2,....,newelementX)
push() method can add one or more elements to the end of the array, and returns the new length.
Syntax:
arrayObject.push(newelement1,newelement2,....,newelementX)
Parameter description:
newelement1: Required. The first element to be added to the array.
newelement2: Optional. The second element to be added to the array.
newelementX: Optional. Multiple elements can be added.
Return value:
The new length after adding the specified value to the array.
Instance:
<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:
arrayObject.pop()
<script type="text/javascript"> var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" document.write(arr) document.write("<br />") document.write(arr.pop()) document.write("<br />") document.write(arr) </script>
Syntax:
arrayObject.unshift(newelement1,newelement2,....,newelementX)
Parameter description:
The new length of arrayObject.
Example:
<script type="text/javascript"> var arr = new Array() arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" document.write(arr + "<br />") document.write(arr.unshift("William") + "<br />") document.write(arr) </script>
Output:
##4, shift()
arrayObject.shift()
<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.shift() + "<br />") document.write(arr) </script>
[Related recommendations: javascript video tutorial
,web front end】
The above is the detailed content of Summarize and organize the push(), pop(), unshift(), and shift() methods in JavaScript. For more information, please follow other related articles on the PHP Chinese website!