Array.prototype.pop()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
pop()
方法會移除並回傳陣列的最後一個元素。此方法會改變陣列的長度。
嘗試一下
const plants = ["broccoli", "cauliflower", "cabbage", "kale", "tomato"];
console.log(plants.pop());
// Expected output: "tomato"
console.log(plants);
// Expected output: Array ["broccoli", "cauliflower", "cabbage", "kale"]
plants.pop();
console.log(plants);
// Expected output: Array ["broccoli", "cauliflower", "cabbage"]
語法
arr.pop()
回傳值
自陣列中移除的元素;若陣列為空,則為 undefined
。
描述
範例
移除陣列的最後一個元素
下面的程式碼為一個包含四個元素的 myFish
陣列,接著移除此陣列的最後一個元素。
js
var myFish = ["angel", "clown", "mandarin", "sturgeon"];
var popped = myFish.pop();
console.log(myFish); // ['angel', 'clown', 'mandarin' ]
console.log(popped); // 'sturgeon'
規範
Specification |
---|
ECMAScript® 2025 Language Specification # sec-array.prototype.pop |
瀏覽器相容性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
pop |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.