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
Discussions

How to push object in array Javascript? - Stack Overflow
I see that you're starting learning JavaScript. I recommend you to read the docs about Arrays and Objects. More on stackoverflow.com
๐ŸŒ stackoverflow.com
javascript - How to use Array.push and return the pushed item? - Stack Overflow
The fact alone that this answer requires an explanation to validate that it causes the same side-effect (due to how the array exotic object modifies its length property when a value is assigned to a slot with an index greater than or equal to its length), I'm pretty sure it would fall into ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Help: Pushing new objects into an array in JavaScript
Dillon Wyatt is having issues with: Hello, I am working through the JavaScript course now. Currently in the "JavaScript Loops, Arrays and Objects". While working on the ob... More on teamtreehouse.com
๐ŸŒ teamtreehouse.com
1
April 22, 2018
How to push Array in to nested Object? I cant use .push() since im trying to add a new key value pair? Ive tried looking at stack overflow and other places but no luck. if anyone can point me in the right direction would be super helpful please :)
Push is an array method but your trying to add data to an object so that isnt going to work. To add data to an obect you need to either: obj.key = ['array'] obj['key'] = ['array'] If an object key is already an array you can: obj.key.push('something) edit: realised it is an array, so the above follows but you want: array[index].key = ['item'] array[index][key] = ['item'], or if already an array array[index][key].push('item) More on reddit.com
๐ŸŒ r/learnjavascript
9
22
March 8, 2022
๐ŸŒ
Shopify
shopify.dev โ€บ docs โ€บ api โ€บ liquid
Liquid reference
The Liquid reference documents the Liquid tags, filters, and objects that you can use to build Shopify themes.
๐ŸŒ
Slack
docs.slack.dev โ€บ methods โ€บ chat.postmessage
chat.postMessage method | Slack Developer Docs
When POSTing with application/x-www-form-urlencoded data, the optional attachments argument should contain a JSON-encoded array of attachments.
๐ŸŒ
NestJS
docs.nestjs.com โ€บ providers
Documentation | NestJS - A progressive Node.js framework
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).
Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ an-idea โ€บ javascript-arrays-push-pop-shift-unshift-adc8fb815fc0
JavaScript Arrays: push(), pop(), shift() & unshift() | by Amanda M Johnson | An Idea (by Ingenious Piece) | Medium
October 10, 2021 - push() adds one or more elements to the end of an array ยท returns the new element(s) is destructive, so the array itself will be mutated ยท relies on the length property to know where to insert the new element(s) and will default to 0 if there ...
๐ŸŒ
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 of the array.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ javascript-arrays
JavaScript Arrays - GeeksforGeeks
The push() method add the element to the end of the array.
Published ย  October 3, 2025
๐ŸŒ
daily.dev
daily.dev โ€บ home โ€บ blog โ€บ get into tech โ€บ pop and push in javascript: array essentials
Pop and Push in JavaScript: Array Essentials
December 22, 2025 - These tools change the original array when you use them. The push() method in JavaScript lets you add one or more items to the end of a list (array) and tells you how long the list is after adding them.
๐ŸŒ
React
react.dev โ€บ reference โ€บ react โ€บ StrictMode
<StrictMode> โ€“ React
This StoryTray component takes an array of stories and adds one last โ€œCreate Storyโ€ item at the end: ... export default function StoryTray({ stories }) { const items = stories; items.push({ id: 'create', label: 'Create Story' }); return ( <ul> {items.map(story => ( <li key={story.id}> {story.label} </li> ))} </ul> ); }
Top answer
1 of 1
1
Objects behave a bit differently from most other datatypes in JavaScript. When you assign an object to a variable or add it to an array or anything like that. JavaScript does not make a copy of the object, it makes the new variable (or array item) contain the exact same object you assigned. To illustrate this take a look at this code: ```JavaScript var example = { message: "Hello" }; var example2 = example; example.message = "world"; console.log(example2); ``` We create an object example then we create a new variable example2 and assign example to it. Then we change the message property of example and then we print out example2. Now what do you think will be printed out? If Objects worked like Strings and Numbers then you would expect { message: 'Hello' } to be printed out. Since that is what example contained when it was assigned to example2. But in reality the above code prints out { message: 'world' }. This is because these two variables don't hold independent copies of the object, they hold the exact same object. Which means that even though there are two variables at play there is actually only one object created in the above code. You just have two variables pointing at it. With that knowledge if you take a look at your code the behavior you experience should make a bit more sense. You create one object at the start of your code, then you assign it the details of one student. Then you add a pointer to that object to the array. Then you change that same object with details of a new student. Since you in reality only have one object you end up changing the values of that one object each time the loop runs. And adding that object to the array multiple times just creates multiple pointers to it. The solution to this issue is quite simple. Just create the object within the loop. Like this: ```JavaScript alert ("Hello"); var students = []; var tempNewStudentStorage; var finished; var questions = [ "What is the student's name?", "What Track(s) are they on?", "What achivement(s) have they gotten?", "What are their points?", ]; while (true) { var newStudent = { Name: "x", Track: "y", Achivement: 0, Points: 0, }; for (var i = 0; i < 4; i +=1) { tempNewStudentStorage = prompt(questions[i]); if (i === 0){ newStudent.Name = tempNewStudentStorage; } else if (i === 1){ newStudent.Track = tempNewStudentStorage; } else if (i === 2){ newStudent.Achievemnt = tempNewStudentStorage; } else if (i === 3){ newStudent.Points = tempNewStudentStorage; }; }; students.push(newStudent); finished = prompt("Are you done adding students? Y/N").toLowerCase(); while (finished.length > 1) { finished = prompt("Are you done adding students? Y/N").toLowerCase() }; if (finished === "y") { alert("Thank you for adding a new student!") break } else if (finished !== "y"){ continue; }; }; console.log(students); ``` That way you actually create a new object each time the loop runs, instead of reusing the object created at the start of the script. It's worth noting that Arrays and Functions also behave in the same way as I described above.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ javascript-array-push-method
JavaScript Array push() Method - GeeksforGeeks
The push() method in JavaScript adds one or more elements to the end of an array and returns the new length of the array.
Published ย  April 15, 2025
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjavascript โ€บ how to push array in to nested object? i cant use .push() since im trying to add a new key value pair? ive tried looking at stack overflow and other places but no luck. if anyone can point me in the right direction would be super helpful please :)
How to push Array in to nested Object? I cant use . ...
March 8, 2022 - Questions and posts about frontend development in general are welcome, as are all posts pertaining to JavaScript on the backend. ... Push is an array method but your trying to add data to an object so that isnt going to work.
๐ŸŒ
W3Schools Blog
w3schools.blog โ€บ home โ€บ javascript array push()
JavaScript Array push() - W3schools
April 20, 2019 - The JavaScript array push() method is used to add one or more elements to the end of an array.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ javascript โ€บ standard-library โ€บ Array โ€บ push
JavaScript Array push() - Add Elements to Array | Vultr Docs
September 27, 2024 - The push() method in JavaScript is an essential tool for manipulating arrays. It allows you to add one or more elements to the end of an array, modifying the array in place and returning the new length of the array.
๐ŸŒ
JavaScript Tutorial
javascripttutorial.net โ€บ home โ€บ javascript array methods โ€บ array.prototype.push()
JavaScript Array Push
November 6, 2024 - The JavaScript Array push() method adds one or more elements to the end of an array and returns the array's length.