--- title: Array.prototype.pop() slug: Web/JavaScript/Reference/Global_Objects/Array/pop tags: - JavaScript - Mảng translation_of: Web/JavaScript/Reference/Global_Objects/Array/pop ---
Phương thức pop()
xoá phần tử cuối cùng của một mảng và trả về phần tử đó. Phương thức này làm thay đổi độ dài của mảng.
arr.pop()
Phần tử bị xoá từ mảng; {{jsxref("undefined")}} nếu mảng rỗng.
The pop
method removes the last element from an array and returns that value to the caller.
Phương thức pop
xoá phần tử cuối cùng của một mảng và trả về giá trị đó cho lời gọi.
pop
is intentionally generic; this method can be {{jsxref("Function.call", "called", "", 1)}} ?or {{jsxref("Function.apply", "applied", "", 1)}} to objects resembling arrays. Objects which do not contain a length
property reflecting the last in a series of consecutive, zero-based numerical properties may not behave in any meaningful manner.
Nếu bạn gọi pop()
trên một mảng rỗng, nó trả về {{jsxref("undefined")}}.
Đoạn mã sau tạo mảng myFish
chứa 4 phần tử, sau đó xoá phần tử cuối cùng.
var myFish = ['angel', 'clown', 'mandarin', 'sturgeon']; var popped = myFish.pop(); console.log(myFish); // ['angel', 'clown', 'mandarin' ] console.log(popped); // 'sturgeon'
Đặc tả | ?Trạng thái | Ghi chú |
---|---|---|
{{SpecName('ES3')}} | {{Spec2('ES3')}} | Định nghĩa lần đầu. Cài đặt trong JavaScript 1.2. |
{{SpecName('ES5.1', '#sec-15.4.4.6', 'Array.prototype.pop')}} | {{Spec2('ES5.1')}} | |
{{SpecName('ES6', '#sec-array.prototype.pop', 'Array.prototype.pop')}} | {{Spec2('ES6')}} | |
{{SpecName('ESDraft', '#sec-array.prototype.pop', 'Array.prototype.pop')}} | {{Spec2('ESDraft')}} |
{{Compat("javascript.builtins.Array.pop")}}