Home  >  Article  >  Web Front-end  >  JavaScript push usage guide_Basic knowledge

JavaScript push usage guide_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 16:28:541633browse

The

push() method adds one or more elements to the end of the array and returns the new length. The return value is the new length after adding the specified value to the array.
Syntax: arrayObject.push(newelement1,newelement2,....,newelementX)
Parameter newelement1, required. The first element to be added to the array.
Parameter newelement2, optional. The second element to be added to the array.
Parameter newelementX, optional. Multiple elements can be added.

The push() method adds its parameters sequentially to the end of arrayObject. It directly modifies the arrayObject instead of creating a new array. The push() method and pop() method use the first-in-last-pop function provided by the array. This method changes the length of the array.

Example:

Copy code The code is as follows:









<script><br>       var btn=document.getElementById('btn');<br>          btn.onclick=function(){<br>         var arrays=new Array();<br>         var checkitem=document.getElementsByName("check");<br> for(var i=0;i<checkitem.length;i )<br />             {<br /> If(checkitem[i].checked){<br />                     arrays.push(checkitem[i].value);//Pass the value in () into the arrays array <br />             }<br />          }<br /> alert(arrays.length)<br /> }<br /> </script>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn