Javascript array push example and Array.prototype.push() tutorial

1. Introduction

In this tutorial, we will learn how to push the element to an array with an example. 

The push method adds the item at the end of an array and returns the new length of an array. 

Note: if you want to add the item at the beginning of the array. you can use unshift() method.

In this example, I will show you multiple ways to add the item to the array for example: adding elements, merging elements, adding multiple items, multidimensional-array push, add an array to the beginning of the array, adding the element at the specific index, etc.

Let's get started

2. Javascript array push with example



The Push() method add the value to the array.

The push method is intentionally generic. The push() method depends on the length property to resolve where to start inserting to the given items. In case, the length property cannot convert into a number then the index will be started with 0 and the new length will be created.

The Push() method can insert multiple elements at once and this method can be used with call() or apply() on objects resembling arrays.

# Adding elements to an array

The following example of Javascript will show you how to add an array. After adding elements to the house variable will give you the new length of an array.

// app.js

var house = ['door', 'bell'];
var totalItems = sports.push('television', 'home-theater');


sports.push() method will append the elements to the existing house object and the output of the above code will be:

// output
// [ 'door', 'bell', 'television', 'home-theater' ]
// 4

# Merging two arrays

The following example of node js will show you how to push all elements from the second array.

var house = ['door', 'bell'];
var furnishedItems = ['sofa', 'dining'];

Array.prototype.push.apply(house, furnishedItems);


The output of the above code will be:

// [ 'door', 'bell', 'sofa', 'dining' ]

apply() method merged both arrays house and furnishedItems and as you can see the above output the combined array is returned.

Note: Do not use apply() method if the second array is large in our case (furnishedItems) Because the maximum number of arguments that one function can manage is limited in practice.


# Add/Push object to an array

 The following example will show you how to push the object to an array

// app.js
arrObject = [
		name: 'Delhi',
		country: 'INDIA'
	    name: 'NY',
	    country: 'USA'

	name: 'Manitoba',
	country: 'Canada'

The output of the above code will be:

// [ { name: 'Delhi', country: 'INDIA' },
//   { name: 'NY', country: 'USA' },
//   { name: 'Manitoba', country: 'Canada' } 
// ]

# Add multiple items in an array

The following example will show how to push multiple items to the array

// app.js
cityArr = [
  ['Delhi', 'Mumbai'],
  ['Bengaluru', 'Pune']
  'Chennai', 'Hyderabad'

The output of the above code will be:

// [ [ 'Delhi', 'Mumbai' ],
//   [ 'Bengaluru', 'Pune' ],
//   [ 'Chennai', 'Hyderabad' ] ]