How to push an Object into an array with JavaScript?

Estimated read time 1 min read

You can push an object into an array in JavaScript by using the push() method. Here is an example:

var fruits = [];
var apple = { type: 'apple', color: 'red' };
fruits.push(apple);

In this example, an object apple is created, and then it is added to the fruits array using the push() method.

Note that push() adds the element to the end of the array, so if you want to insert an element at a specific position in the array, you can use the splice() method.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply