push() 方法用于將一個或多個元素添加到數組的末尾,并返回新的長度。
示例:
var fruits = ["apple", "banana"];
var length = fruits.push("orange", "grape");
console.log(fruits); // ["apple", "banana", "orange", "grape"]
console.log(length); // 4
在上面的例子中,我們首先創建了一個包含兩個元素的數組 fruits。然后我們使用 push() 方法將兩個元素 "orange" 和 "grape" 添加到數組的末尾。push() 方法返回新的數組長度,我們將其存儲在變量 length 中。
最后,我們分別打印了數組 fruits 和變量 length 的值??梢钥吹?,數組 fruits 現在包含了四個元素,而變量 length 的值為 4。