[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/push [Back]  [Original]

Array.prototype.push() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Array.prototype.push()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 7.

Array push() .

In this article

const animals = ["pigs", "goats", "sheep"];

const count = animals.push("cows");
console.log(count);
// Expected output: 4
console.log(animals);
// Expected output: Array ["pigs", "goats", "sheep", "cows"]

animals.push("chickens", "cats", "dogs");
console.log(animals);
// Expected output: Array ["pigs", "goats", "sheep", "cows", "chickens", "cats", "dogs"]

js
push()
push(element1)
push(element1, element2)
push(element1, element2, /* , */ elementN)

element1, , elementN

().

length .

push .

Array.prototype.unshift() push() .

push() . this . this [arr.concat([element0, element1, /* ... ,*/ elementN])] . concat() .

push() . this length . , .

2 sports 2 . total .

js
const sports = ["soccer", "baseball"];
const total = sports.push("football", "swimming");

console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']
console.log(total); // 4

spread syntax .

js
const vegetables = ["parsnip", "potato"];
const moreVegs = ["celery", "beetroot"];

//       
vegetables.push(...moreVegs);

console.log(vegetables); // ['parsnip', 'potato', 'celery', 'beetroot']

concat() .

push()

push() this length . , push() this length . , length length .

js
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 .

js
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 .

Specification
ECMAScript 2027 LanguageSpecification
# sec-array.prototype.push


Web Proxy Viewer  |  New URL  |  Original Page