TypeError: forEach is not a function in JavaScript

December 8, 2022

TypeError: forEach is not a function in JavaScript

This guide is part of the “Common JavaScript Errors” series. It’s focused entirely on providing quick and easy solutions for JavaScript-related problems.


The "TypeError: forEach is not a function" error is thrown when the code attempts to call the forEach() method on a value that is not an array or an array-like object. The forEach() method is a method that is defined on the Array.prototype, so it can only be called on an array or an object that inherits from the Array class.

For example, if you try to call the forEach() method on a regular JavaScript object, like this:

const obj = { "name": "John", "age": 30 };
obj.forEach(function(value, key) {
  console.log(key + ": " + value);
});

The above example will throw the "TypeError: forEach is not a function" error, because the obj object is not an array or an array-like object, and therefore does not have the forEach() method.

Two ways to deal with this problem is to use Array.from or the Object.keys method.

Here is an example for Array.from:

const arrayLike = {
  0: 'a',
  1: 'b',
  2: 'c',
  length: 3
};

const arr = Array.from(arrayLike);
arr.forEach(element => console.log(element));

// output: a b c

In this example, we first check if the obj value is an array using the Array.isArray() method. If it is an array, we call the forEach() method on it. Otherwise, we do not call forEach() and the error is avoided.

The Array.from() method creates a new, shallow-copied Array instance from an array-like or iterable object. In this case, we're passing the arrayLike object as the argument to Array.from(), which creates a new array with the same elements as the arrayLike object.

Then, we're using the forEach() method to iterate over the elements of the arr array and log each element to the console.

And here's an example using Object.keys:

const arrayLike = {
  0: 'a',
  1: 'b',
  2: 'c',
  length: 3
};

const arr = Object.keys(arrayLike);
arr.forEach(element => console.log(element));

// output: 0 1 2 length

The Object.keys() method returns an array of a given object's own enumerable property names, in the same order as we get with a normal loop. In this case, we're passing the arrayLike object as the argument to Object.keys(), which creates a new array with the keys of the arrayLike object.

Checking for the correct type

To check if a value is of the correct type before calling the forEach() method, you can use the instanceof operator to check the type of the value.

Here's an example of how you can do that:

const arrayLike = {
  0: 'a',
  1: 'b',
  2: 'c',
  length: 3
};

if (arrayLike instanceof Array) {
  // The arrayLike value is an array, so it's safe to call forEach() on it.
  arrayLike.forEach(element => console.log(element));
} else {
  // The arrayLike value is not an array, so it's not safe to call forEach() on it.
  // You can throw an error or do something else here.
  throw new TypeError('The value is not an array.');
}

In this example, the arrayLike variable is an object with some properties resembling an array. However, it is not an actual array because it's not an instance of the Array class. As such, an error is thrown.