How to push an object in an Array

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

animals.push({animal: 'cows'});

console.log(animals); // ["pigs", "goats", "sheep", { animal: "cows" }]
Answer from Soham on Stack Overflow
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Array โ€บ push
Array.prototype.push() - JavaScript | MDN
Merging two arrays can also be done with the concat() method. 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().
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ home โ€บ javascript โ€บ javascript array push method
Understanding JavaScript Array push Method
September 1, 2008 - The JavaScript Array push() method ... array and returns a new length after insertion. For Example, myArr.push('x', 'y') adds 'x' and 'y' to the end of myArr....
๐ŸŒ
DEV Community
dev.to โ€บ technoph1le โ€บ the-javascript-arraypush-method-explained-5d4m
The JavaScript `Array.push()` method explained - DEV Community
November 24, 2022 - The Array.push() method adds one or more elements to the end of an array and returns the new length... Tagged with webdev, beginners, javascript.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ javascript-array-push-method
JavaScript Array push() Method - GeeksforGeeks
Example: In this example, function func() initializes an array with elements 'GFG', 'gfg', 'g4g', then pushes the string GeeksforGeeks to the end of the array using the push() method.
Published ย  April 15, 2025
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ javascript โ€บ standard-library โ€บ Array โ€บ push
JavaScript Array push() - Add Elements to Array | Vultr Docs
September 27, 2024 - Here, 4, 5, 6 are added to the numbers array in a single method call. The new length of the array is outputted, showing how push() has effectively increased the number of elements.
Find elsewhere
๐ŸŒ
Tabnine
tabnine.com โ€บ home โ€บ how to use the array push() method in javascript
How to Use the Array push() Method in JavaScript - Tabnine
July 25, 2024 - The push() method returns the length of the new array after the items have been added: const nums = [1, 2, 3, 4, 5]; const newLength = nums.push(6); console.log(nums); // Expected output: [1, 2, 3, 4, 5, 6] console.log(newLength); // Expected ...
๐ŸŒ
JavaScript Tutorial
javascripttutorial.net โ€บ home โ€บ javascript array methods โ€บ array.prototype.push()
JavaScript Array Push
November 6, 2024 - let colors = ['red', 'green', 'blue']; let cmyk = ['cyan', 'magenta', 'yellow', 'back'];Code language: JavaScript (javascript) And you want to append the elements of the cmyk to the colors array.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 46116204 โ€บ javascript-how-to-use-the-push-method
arrays - Javascript: how to use the push method? - Stack Overflow
it('should add elements at the ... value gets added to the array at the end (last position) nums.push(4); expect(nums).toEqual(); expect(nums.length).toEqual(); // Note that we don't save what push returns, we just call ...
๐ŸŒ
Javatpoint
javatpoint.com โ€บ javascript-array-push-method
JavaScript Array push() method
JavaScript Array Method The method creates and returns a new iterator object which holds the key for every index in the array. This method does not affect the original array. Syntax array.; Parameter It does not hold any parameter. Return It returns a new array iterator object. JavaScript Array Example Let's...
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-insert-an-element-into-an-array-in-javascript
Push into an Array in JavaScript โ€“ How to Insert an Element into an Array in JS
November 7, 2024 - The push() method is similar to the unshift() method as it adds an element to the end of an array rather than the beginning. It returns the length of the new array and, like the unshift() method, can be used to add more than one element.
Top answer
1 of 16
5428

Use the Array.prototype.push method to append values to the end of an array:

Copy// initialize array
var arr = [
  "Hi",
  "Hello",
  "Bonjour"
];

// append new value to the array
arr.push("Hola");

console.log(arr);
Run code snippetEdit code snippet Hide Results Copy to answer Expand


You can use the push() function to append more than one value to an array in a single call:

Copy// initialize array
var arr = ["Hi", "Hello", "Bonjour", "Hola"];

// append multiple values to the array
arr.push("Salut", "Hey");

// display all values
for (var i = 0; i < arr.length; i++) {
  console.log(arr[i]);
}
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Note that the push() method returns the updated length of the array.


Update

If you want to add the items of one array to another array, you can use firstArray.concat(secondArray):

Copyvar arr = [
  "apple",
  "banana",
  "cherry"
];

// Do not forget to assign the result as, unlike push, concat does not change the existing array
arr = arr.concat([
  "dragonfruit",
  "elderberry",
  "fig"
]);

console.log(arr);
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Update

Just an addition to this answer if you want to prepend any value to the start of an array (i.e. first index) then you can use Array.prototype.unshift for this purpose.

Copyvar arr = [1, 2, 3];
arr.unshift(0);
console.log(arr);
Run code snippetEdit code snippet Hide Results Copy to answer Expand

It also supports appending multiple values at once just like push.


Update

Another way with ES6 syntax is to return a new array with the spread syntax. This leaves the original array unchanged, but returns a new array with new items appended or prepended, compliant with the spirit of functional programming.

Copyconst arr1 = [
  "Hi",
  "Hello",
  "Bonjour",
];
const arr2 = [
  "Ciao",
  "Hej",
  "Merhaba",
];

const newArr1 = [
  ...arr1,
  "Salut",
];
const newArr2 = [
  "Salut",
  ...arr2,
];
const newArr3 = [
  ...arr1,
  ...arr2,
];

console.log(newArr1, newArr2, newArr3);
Run code snippetEdit code snippet Hide Results Copy to answer Expand

2 of 16
1122

If you're only appending a single variable, then push() works just fine. If you need to append another array, use concat():

Copyvar ar1 = [1, 2, 3];
var ar2 = [4, 5, 6];

var ar3 = ar1.concat(ar2);

alert(ar1);
alert(ar2);
alert(ar3);
Run code snippetEdit code snippet Hide Results Copy to answer Expand

The concat does not affect ar1 and ar2 unless reassigned, for example:

Copyvar ar1 = [1, 2, 3];
var ar2 = [4, 5, 6];

ar1 = ar1.concat(ar2);
alert(ar1);
Run code snippetEdit code snippet Hide Results Copy to answer Expand

There is a lot of great information on JavaScript Reference.

๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ javascript โ€บ arrays โ€บ .push()
JavaScript | Arrays | .push() | Codecademy
July 11, 2022 - The .push() method adds one or more elements to the end of an array and returns the new length. ... Front-end engineers work closely with designers to make websites beautiful, functional, and fast. ... Learn how to use JavaScript โ€” a powerful and flexible programming language for adding website ...
๐ŸŒ
Shiksha
shiksha.com โ€บ home โ€บ it & software โ€บ it & software articles โ€บ programming articles โ€บ javascript array push
JavaScript Array Push - Shiksha Online
December 18, 2023 - The JavaScript array.push() method adds one or more elements to the end of an array, directly modifying the original array.
๐ŸŒ
ZetCode
zetcode.com โ€บ js-array โ€บ push
JavaScript push - adding elements to arrays in JS
In this article we have demonstrated how to use the push() method to add elements to arrays in JavaScript.