這篇文章主要介紹了JavaScript使用pop方法移除數組最后一個元素,實例分析了javascript中pop方法的使用技巧,需要的朋友可以參考下
本文實例講述了JavaScript使用pop方法移除數組最后一個元素的用法。分享給大家供大家參考。具體如下:
下面的代碼演示了JS數組的pop方法,可以用來移除數組的最后一個元素,實際上就是把數組當成堆棧使用
- <!DOCTYPE html>
- <html>
- <body>
- <p id="demo">
- Click the button to remove the last array element.
- </p>
- <button onclick="myFunction()">Try it</button>
- <script>
- var fruits = ["Banana","Orange","Apple","Mango"];
- function myFunction()
- {
- fruits.pop();
- var x=document.getElementById("demo");
- x.innerHTML=fruits;
- }
- </script>
- </body>
- </html>
運行結果如下:
Banana,Orange,Apple
希望本文所述對大家的javascript程序設計有所幫助。
新聞熱點
疑難解答
圖片精選