Định nghĩa Array push() trong JavaScript

Phương thức array push() trong Javascript thêm một hoặc nhiều phần tử tới phần cuối của một mảng và trả về độ dài mới của mảng.

Cú pháp Array push() trong JavaScript

Cú pháp của nó như sau:

array.push(element1, ..., elementN);

Tham số

  • element1, ..., elementN: The elements to add to the end of the array.

Trả về giá trị

  • Trả về độ dài của mảng mới.

Ví dụ Array push() trong JavaScript

<html>
   <head>
      <title>JavaScript Array push Method</title>
   </head>

   <body>

      <script type="text/javascript">
         var numbers = new Array(1, 4, 9);

         var length = numbers.push(10);
         document.write("new numbers is : " + numbers ); 

         length = numbers.push(20);
         document.write("<br />new numbers is : " + numbers ); 
      </script>

   </body>
</html>

Kết quả

new numbers is : 1,4,9,10
new numbers is : 1,4,9,10,20 

Viết câu trả lời

Drop Images

0 Bình luận