下面的代碼首先創建了一個擁有四個元素的數組 myFish,然后刪除掉它的最后一個元素。
let myFish = ["angel", "clown", "mandarin", "surgeon"]; let popped = myFish.pop(); console.log(myFish); // ["angel", "clown", "mandarin"] console.log(popped); // surgeon
let a = [1, 2, 3]; a.length; // 3 a.pop(); // 3 console.log(a); // [1, 2] a.length; // 2
語法
arr.pop()
返回值
從數組中刪除的元素(當數組為空時返回undefined)。
描述
pop 方法從一個數組中刪除並返回最后一個元素。
pop 方法有意具有通用性。該方法和 call() 或 apply() 一起使用時,可應用在類似數組的對象上。pop方法根據 length屬性來確定最后一個元素的位置。如果不包含length屬性或length屬性不能被轉成一個數值,會將length置為0,並返回undefined。
如果你在一個空數組上調用 pop(),它返回 undefined。


