pop()
方法从数组中删除最后一个元素,并返回该元素的值。此方法更改数组的长度。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
arr.pop()
从数组中删除的元素(当数组为空时返回undefined
)。
pop
方法从一个数组中删除并返回最后一个元素。
pop
方法有意具有通用性。该方法和 call()
或 apply()
一起使用时,可应用在类似数组的对象上。pop
方法根据 length
属性来确定最后一个元素的位置。如果不包含length
属性或length
属性不能被转成一个数值,会将length
置为0,并返回undefined
。
如果你在一个空数组上调用 pop(),它返回 undefined
。
下面的代码首先创建了一个拥有四个元素的数组 myFish,然后删除掉它的最后一个元素。
let myFish = ["angel", "clown", "mandarin", "surgeon"];
let popped = myFish.pop();
console.log(myFish);
// ["angel", "clown", "mandarin"]
console.log(popped);
// surgeon
Specification | Status | Comment |
---|---|---|
ECMAScript 3rd Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.2. |
ECMAScript 5.1 (ECMA-262) Array.prototype.pop |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) Array.prototype.pop |
Standard | |
ECMAScript Latest Draft (ECMA-262) Array.prototype.pop |
Draft |
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
pop | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 5.5 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |