Aplicações Java para web com JSF e JPA
Aplicações Java para web com JSF e JPA
Array.prototype.push()
Baseline Widely available
The push() method of Array instances adds the specified elements to the end of
an array and returns the new length of the array.
Try it
JavaScript Demo: Array.prototype.push()
1 const animals = ["pigs", "goats", "sheep"];
2
3 const count = animals.push("cows");
4 console.log(count);
5 // Expected output: 4
6 console.log(animals);
7 // Expected output: Array ["pigs", "goats", "sheep", "cows"]
8
9 animals.push("chickens", "cats", "dogs");
10 console.log(animals);
11 // Expected output: Array ["pigs", "goats", "sheep", "cows", "chickens",
"cats", "dogs"]
12
Run
Reset
Syntax
JS
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push 1/6
27/06/2025, 20:30 Array.prototype.push() - JavaScript | MDN
push()
push(element1)
push(element1, element2)
push(element1, element2, /* …, */ elementN)
Parameters
element1 , …, elementN
The element(s) to add to the end of the array.
Return value
The new length property of the object upon which the method was called.
Description
The push() method appends values to an array.
Array.prototype.unshift() has similar behavior to push() , but applied to the start
of an array.
The push() method is a mutating method. It changes the length and the content
of this . In case you want the value of this to be the same, but return a new
array with elements appended to the end, you can use arr.concat([element0,
element1, /* ... ,*/ elementN]) instead. Notice that the elements are wrapped in
Examples
Adding elements to an array
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push 2/6
27/06/2025, 20:30 Array.prototype.push() - JavaScript | MDN
The following code creates the sports array containing two elements, then
appends two elements to it. The total variable contains the new length of the
array.
JS
Merging two arrays can also be done with the concat() method.
Calling push() on non-array objects
The push() method reads the length property of this . It then sets each index of
this starting at length with the arguments passed to push() . Finally, it sets the
JS
const arrayLike = {
length: 3,
unrelated: "foo",
2: 4,
};
Array.prototype.push.call(arrayLike, 1, 2);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push 3/6
27/06/2025, 20:30 Array.prototype.push() - JavaScript | MDN
console.log(arrayLike);
// { '2': 4, '3': 1, '4': 2, length: 5, unrelated: 'foo' }
const obj = {
length: 0,
addElem(elem) {
// obj.length is automatically incremented
// every time an element is added.
[].push.call(this, elem);
},
};
Note that although obj is not an array, the method push successfully incremented
obj 's length property just like if we were dealing with an actual array.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push 4/6
Specifications
27/06/2025, 20:30 Array.prototype.push() - JavaScript | MDN
Specification
ECMAScript® 2026 Language Specification
# sec-array.prototype.push
Browser compatibility
Report problems with this compatibility data • View data on GitHub
diordnA weiVbeW
tenretnI gnusmaS
diordnA emorhC
SOi no weiVbeW
diordnA arepO
SOi no irafaS
emorhC
sj.edoN
xoferiF
arepO
irafaS
oneD
egdE
push
1 12 1 4 1 18 4 10.1 1 1 1 1 1 0.10
See also
Polyfill of Array.prototype.push in core-js with fixes of this method
es-shims polyfill of Array.prototype.push
Indexed collections guide
Array
Array.prototype.pop()
Array.prototype.shift()
Array.prototype.unshift()
Array.prototype.concat()
Array.prototype.splice()
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push 6/6