How to Get Around .Push Is Not a Function in JavaScript

Uncaught TypeError: data.push is not a function

To use the push function of an Array your var needs to be an Array.

Change data{"name":"ananta","age":"15"} to following:

var data = [
"name": "ananta",
"age": "15",
"country": "Atlanta"

data.push({"name": "Tony Montana", "age": "99"});

data.push({"country": "IN"});


The containing Array Items will be typeof Object and you can do following:

var text = "You are " + data[0]->age + " old and come from " + data[0]->country;

Notice: Try to be consistent. In my example, one array contained object properties name and age while the other only contains country. If I iterate this with for or forEach then I can't always check for one property, because my example contains Items that changing.

Perfect would be: data.push({ "name": "Max", "age": "5", "country": "Anywhere" } );

So you can iterate and always can get the properties, even if they are empty, null or undefined.


Cool stuff to know:

var array = new Array();

is similar to:

var array = [];


var object = new Object();

is similar to:

var object = {};

You also can combine them:

var objectArray = [{}, {}, {}];

Javascript push is not a function

It means that your storeArray[storename] is not an array.

storeArray[storename] =[i];

you just assign a value to the storeArray[storename] which is not an array. You need first to create an array and then put that element into it.

storeArray[storename] = [[i] ];

and at the next iterations you will have an array with one element and can use push on it.

Array.push is not a function with return Array

As others have pointed out, you're replacing the collections array with a single object containing the updated element for the given character. You should just update that element in place rather than reassign the array itself by replacing that assignment with:


Your function can also be simplified by replacing the loop with the find() method.

checkuniqueness(collection, character) {
let counter = collection.find(o => o.character == character);
if (counter) {
} else {
character: character,
number: 1

return collection;

Push is not a function JavaScript error

This will create your example.

var myArray = new Array(4);
for (var i = 0; i < myArray.length; i++) {
myArray[i] = ["ID", "SomeValue" + (i+1)];

But if you need to set data from a database, how is that being set in the Javascript? if it's in a different array you could do the following:

var dbArray = ["SomeValue1", "SomeValue2", "SomeValue3"];

var myArray = new Array(dbArray.length);
for (var i = 0; i < myArray.length; i++) {
myArray[i] = ["ID", dbArray[i]];

Javascript: .push is not a function

Array.push doesn't return an array. It returns the new length of the array it was called on.

So, your return array.push(b); returns an int. That int gets passed back as array... which is not an array so it doesn't have a .push() method.

You need to do:

return array;

I am getting error push() is not a function while pushing element in array?

You should delete the [i] in abc[i].push() because abc[i] is string, but the .push() method works for arrays. Therefore you should use abc.push() instead of abc[i].push().

Related Topics

Leave a reply