Ad Space — Top Banner

X is not iterable

JavaScript Programming Language

Severity: Moderate

What Does This Error Mean?

This error means you tried to loop over something that cannot be looped over. JavaScript lets you use for...of loops and spread syntax (...) on things called 'iterables' — arrays, strings, Maps, Sets, and similar. But if you try to loop over a plain object, a number, null, or undefined, JavaScript says it is 'not iterable' and crashes. The fix is to make sure you are looping over an array or other iterable, not a plain object.

Affected Models

  • All browsers
  • Node.js
  • All JavaScript environments

Common Causes

  • Using for...of on a plain object {} instead of an array — plain objects are not iterable
  • An API returned an object instead of an array, and you tried to loop over it directly
  • A variable expected to hold an array is null or undefined
  • Using array destructuring or spread syntax on something that is not an array
  • Trying to use for...of on a number or boolean value

How to Fix It

  1. Check what type the variable actually holds before looping. Use console.log(typeof myVar) and console.log(Array.isArray(myVar)) to see if it is an array.

    If it is an object, you cannot use for...of directly — you need to loop over its keys or values differently.

  2. To loop over a plain object, use for...in (which loops over keys) or Object.keys(), Object.values(), or Object.entries().

    Example: for (const [key, value] of Object.entries(myObject)) { console.log(key, value); } — this converts the object to an iterable array of key-value pairs.

  3. If the variable comes from an API response, check that it is an array before looping: if (Array.isArray(data)) { for (const item of data) { ... } }

    APIs sometimes return a single object instead of an array when there is only one result, or an object wrapper like { results: [...] } instead of the array directly.

  4. If the variable might be null or undefined, provide a fallback: for (const item of (myArray || [])) — this uses an empty array if myArray is falsy.

    This prevents the crash but means the loop body never runs. Make sure that is the right behavior for your situation.

  5. If you are using destructuring with an API response, make sure the destructured value is an array: const [first, second] = myData — this only works if myData is an array or iterable.

    If myData is an object, use object destructuring instead: const { first, second } = myData

When to Call a Professional

This is a common error when working with data from APIs or when mixing up objects and arrays. It is always fixable — check what type the variable actually holds before looping over it.

Frequently Asked Questions

What is an iterable in JavaScript?

An iterable is any object that JavaScript knows how to loop over. Arrays, strings, Maps, Sets, and NodeLists are all iterables. Plain objects ({}) are NOT iterable by default — they have no built-in way to say 'give me the next item'. To loop over a plain object, use Object.keys(), Object.values(), or Object.entries().

What is the difference between for...of and for...in?

for...of loops over the values of an iterable (works with arrays, strings, Maps, Sets). for...in loops over the keys (property names) of an object. for...in works with plain objects; for...of does not. For arrays, prefer for...of or a regular for loop — for...in on arrays can have surprising behavior.

Why does my spread syntax (...myArray) cause this error?

Spread syntax requires an iterable — it expands an iterable into individual items. If myArray is actually a plain object or undefined, spread syntax will throw 'not iterable'. Check that the variable is actually an array with Array.isArray() before spreading it.