您现在的位置: 万盛学电脑网 >> 程序编程 >> 脚本专题 >> javascript >> 正文

JavaScript使用pop方法移除数组最后一个元素用法实例

作者:佚名    责任编辑:admin    更新时间:2022-06-22

 下面的代码演示了JS数组的pop方法,可以用来移除数组的最后一个元素,实际上就是把数组当成堆栈使用

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <!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