How to solve the “TypeError: array.splice is not a function” when ‘var array = {}’?

How to solve the “TypeError: array.splice is not a function” when ‘var array = {}’?

The splice() method is used to add or remove elements from an array. It is not a method that is available on objects.

If you get the error “TypeError: array.splice is not a function”, it means that you are trying to call the splice() method on an object that is not an array.

let array = {};
array.splice(0, 1);  // TypeError: array.splice is not a function

To solve this error, you will need to make sure that you are calling the splice() method on an array, not on an object.

For example:

let array = [1, 2, 3];
array.splice(0, 1);  // [1]
console.log(array);  // [2, 3]

Leave a Reply