Array.prototype.pop()

pop() 方法會移除並回傳陣列的最後一個元素。此方法會改變陣列的長度。

語法

arr.pop()

回傳值

自陣列中移除的元素;若陣列為空,則為 undefined

描述

pop 方法會移除陣列中的最後一個元素,並將該值回傳給呼叫者。

pop 方法被刻意設計為具通用性;此方法可以藉由 calledapplied 應用於類似陣列的物件上。若欲應用此方法的物件不包含代表一系列啟始為零之數字屬性序列長度的 length 屬性,可能是不具任何意義的行為。

如果於空陣列呼叫 pop(),將會回傳 undefined

範例

移除陣列的最後一個元素

下面的程式碼為一個包含四個元素的 myFish 陣列,接著移除此陣列的最後一個元素。

var myFish = ['angel', 'clown', 'mandarin', 'sturgeon'];

var popped = myFish.pop();

console.log(myFish); // ['angel', 'clown', 'mandarin' ] 

console.log(popped); // 'sturgeon'

規範

Specification Status Comment
ECMAScript 3rd Edition (ECMA-262) Standard Initial definition. Implemented in JavaScript 1.2.
ECMAScript 5.1 (ECMA-262)
The definition of 'Array.prototype.pop' in that specification.
Standard  
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Array.prototype.pop' in that specification.
Standard  
ECMAScript (ECMA-262)
The definition of 'Array.prototype.pop' in that specification.
Living Standard  

瀏覽器相容性

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
popChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 5.5Opera Full support 4Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support 0.1.100

Legend

Full support  
Full support

參見