TypeError: push is not a function in JavaScript [Solved]

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
3 min

banner

# TypeError: push is not a function in JavaScript [Solved]

The "TypeError: push is not a function" occurs when the push() method is called on a value that is not an array.

To solve the error, convert the value to an array before calling the method, or make sure to only call the push() method on valid arrays.

typeerror push is not a function

shell
Uncaught TypeError: object.push is not a function

Here is an example of how the error occurs.

index.js
const arr = {name: 'Tom'}; // โ›”๏ธ Uncaught TypeError: arr.push is not a function arr.push({name: 'James'});

array push is not a function

We called the Array.push() method on an object which caused the error.

# Pushing objects into an array

In this situation, we should place the object in an array, so we are able to push new objects into the array.

index.js
const arr = [{name: 'Tom'}]; arr.push({name: 'James'}); // ๐Ÿ‘‡๏ธ [{name: 'Tom'}, {name: 'James'}] console.log(arr);

pushing objects into an array

The code for this article is available on GitHub
If the object is an element in an array, we can use the Array.push method to add other objects to the end of the array.

# Adding key-value pairs to an object

If you need to add key-value pairs to an object, use bracket or dot notation.

index.js
const obj = { id: 1, name: 'Alice', }; // ๐Ÿ‘‡๏ธ dot notation obj.country = 'Chile'; // ๐Ÿ‘‡๏ธ bracket notation obj['street address'] = 'Example street 123'; // ๐Ÿ‘‡๏ธ {id: 1, name: 'Alice', country: 'Chile', 'street address': ...} console.log(obj);

adding key value pairs to object

The code for this article is available on GitHub

A general rule of thumb is to use bracket notation if the object's key contains a space, hyphen or starts with a digit or a special character. In all other cases, use dot notation as it is more concise and easier to read.

# Convert the value to an array before calling push()

If you got the error when working with a NodeList or another array-like object, convert the array-like object to an array before calling the push() method.

index.js
const set = new Set(['a', 'b']); const arr = Array.from(set); console.log(arr); // ๐Ÿ‘‰๏ธ ['a', 'b'] arr.push('c'); console.log(arr); // ๐Ÿ‘‰๏ธ ['a', 'b', 'c']

convert the value to an array before calling push

The code for this article is available on GitHub

We used the Array.from() method to convert a Set object to an array, so we can call the push() method on the array.

You can also use the spread syntax (...) to convert a value to an array.

index.js
const set = new Set(['a', 'b']); const arr = [...set]; console.log(arr); // ๐Ÿ‘‰๏ธ ['a', 'b'] arr.push('c'); console.log(arr); // ๐Ÿ‘‰๏ธ ['a', 'b', 'c']

We used the spread syntax (...) to unpack the values of the Set into a new array before calling push().

If the error persists, console.log the value you're calling the push() method on and make sure it's an array.

# Check if the value is an array before calling push()

You can also conditionally check if the value is an array before calling the push() method to avoid getting errors.

index.js
const arr = null; if (Array.isArray(arr)) { arr.push('example'); } if (Array.isArray(arr)) { arr.push({id: 2, name: 'Brad'}); }
The code for this article is available on GitHub

We used the Array.isArray method to check if the value is an array before calling the push method.

# Access a property on an object before calling push()

If have an object and you want to push values to a specific property that stores an array, access the property and call the push() method on it.

index.js
const obj = { numbers: [1, 2], }; obj.numbers.push(3); // ๐Ÿ‘‡๏ธ {numbers: [1, 2, 3]} console.log(obj);
The code for this article is available on GitHub

We accessed the numbers property, which stores an array and called the push() method on the array.

# Solve the error if using local storage

If you use local storage to store an array, make sure to parse the array before calling the Array.push() method.

index.js
const arr = ['bobby', 'hadz']; localStorage.setItem('site', JSON.stringify(arr)); const arrAgain = JSON.parse(localStorage.getItem('site')); console.log(arrAgain); // ๐Ÿ‘‰๏ธ ['bobby', 'hadz'] arrAgain.push('com'); console.log(arrAgain); // ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com']

We used the JSON.stringify() method to convert an array to a JSON string and added the string to local storage.

The next step is to use the JSON.parse() method to parse the string into an array.

Lastly, we called the push() method on the array.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev