Fix JavaScript TypeError: X is not iterable (2025 Guide)

Fix TypeError: X is not iterable in JavaScript - 2025 Guide
AI-generated image of developer fixing JavaScript TypeError: X is not iterable with error message on laptop screen

Fix TypeError: X is not iterable in JavaScript - 2025 Guide

Posted on: March 12, 2025

If you’ve encountered a "TypeError: X is not iterable" in JavaScript, it means you’re trying to loop over something that can’t be iterated. In this 2025 guide, we’ll break down why this happens and how to fix it quickly.

What Causes "TypeError: X is not iterable"?

This error occurs when you attempt to iterate over a value that isn’t iterable (like an array or string). Common causes include:

  • Non-Iterable Object: Trying to use for...of on a plain object.
  • Undefined/Null Value: Attempting to iterate over a variable that’s undefined or null.
  • Incorrect Method: Using a method like Array.from() on a non-iterable value.

Here’s an interactive example (open your browser console with F12 to see the error):

In this example, obj is a plain object, which isn’t iterable, so using for...of triggers the error.

How to Fix It: 3 Solutions

Let’s tackle this error with practical steps:

Diagram showing steps to fix JavaScript TypeError: X is not iterable

(Diagram: Developer runs code, gets error, checks value/iterable, fixes issue.)

Solution 1: Use the Correct Loop for Objects

Plain objects aren’t iterable with for...of. Use for...in or Object.keys() instead:

// Wrong
const obj = { name: "John" };
for (let value of obj) {
  console.log(value); // TypeError: obj is not iterable
}

// Fixed
const obj2 = { name: "John" };
for (let key in obj2) {
  console.log(obj2[key]);
}
// Or
Object.keys(obj2).forEach(key => console.log(obj2[key]));

for...in loops over object keys, while Object.keys() gives an iterable array of keys.

Solution 2: Check for Undefined/Null

Ensure the value is iterable before looping:

// Wrong
let data = undefined;
for (let item of data) {
  console.log(item); // TypeError: data is not iterable
}

// Fixed
let data2 = undefined;
if (Array.isArray(data2) || typeof data2[Symbol.iterator] === 'function') {
  for (let item of data2) {
    console.log(item);
  }
} else {
  console.log("Data is not iterable");
}

Check if the value is iterable using Symbol.iterator or Array.isArray().

Solution 3: Convert to Iterable

If you need to iterate, convert the value to an iterable type:

// Wrong
const obj = { name: "John" };
Array.from(obj); // TypeError: obj is not iterable

// Fixed
const obj2 = { name: "John" };
const values = Object.values(obj2); // Converts to array
for (let value of values) {
  console.log(value);
}

Use Object.values() or Object.entries() to make an iterable array from an object.

Quick Checklist

  • Is the value an iterable (like an array or string)? (Check with Symbol.iterator)
  • Is the value undefined or null? (Add a check)
  • Can you convert the value to an iterable? (Use Object.values())

Conclusion

The "TypeError: X is not iterable" error can be a pain, but with proper checks and conversions, you’ll keep your loops running smoothly. These 2025 solutions will get you sorted fast. Got another JavaScript error on your mind? Let us know in the comments!

Comments

Popular posts from this blog

Fix Python SystemExit (2025 Guide)

Fix Python UnicodeTranslateError (2025 Guide)

Fix Python UnicodeEncodeError (2025 Guide)

Fix Next.js Error: fetch failed due to Network or CORS Issues (2025 Guide)

Fix Python ConnectionAbortedError (2025 Guide)