| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/push | [Back] [Original] |
Get to know MDN better
const animals = ["pigs", "goats", "sheep"];
const count = animals.push("cows");
console.log(count);
// : 4
console.log(animals);
// : Array ["pigs", "goats", "sheep", "cows"]
animals.push("chickens", "cats", "dogs");
console.log(animals);
// : Array ["pigs", "goats", "sheep", "cows", "chickens", "cats", "dogs"]
push()
push(element1)
push(element1, element2)
push(element1, element2, /* , */ elementN)
push()
Array.prototype.unshift() push()
push() this this arr.concat([element0, element1, /* ... ,*/ elementN]) concat()
2 sports 2 total 4
const sports = ["soccer", "baseball"];
const total = sports.push("football", "swimming");
console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']
console.log(total); // 4
const vegetables = ["parsnip", "potato"];
const moreVegs = ["celery", "beetroot"];
// 1 2
vegetables.push(...moreVegs);
console.log(vegetables); // ['parsnip', 'potato', 'celery', 'beetroot']
2 concat()
push() this length length this push() length
const arrayLike = {
length: 3,
unrelated: "foo",
2: 4,
};
Array.prototype.push.call(arrayLike, 1, 2);
console.log(arrayLike);
// { '2': 4, '3': 1, '4': 2, length: 5, unrelated: 'foo' }
const plainObj = {};
// length 0
Array.prototype.push.call(plainObj, 1, 2);
console.log(plainObj);
// { '0': 1, '1': 2, length: 2 }
push Array.prototype.push
Array.prototype.push call JavaScript
const obj = {
length: 0,
addElem(elem) {
// obj.length
//
[].push.call(this, elem);
},
};
//
obj.addElem({});
obj.addElem({});
console.log(obj.length); // 2
obj push obj length
| ECMAScript 2027 LanguageSpecification # sec-array.prototype.push |
Arrayat()concat()copyWithin()entries()every()fill()filter()find()findIndex()findLast()findLastIndex()flat()flatMap()forEach()includes()indexOf()join()keys()lastIndexOf()map()pop()push()reduce()reduceRight()reverse()shift()slice()some()sort()splice()toLocaleString()toReversed()toSorted()toSpliced()toString()unshift()values()with()[Symbol.iterator]()Object/Function| Web Proxy Viewer | New URL | Original Page |