Skip to content

TypeError: x is not iterable

Error message

                x is not iterable (cannot read property Symbol(Symbol.iterator))
              

Quick summary

You tried to iterate over something that is not iterable — for...of and spread only work on arrays, strings, Maps, Sets, and other iterables.

Why this happens

  • Used for...of on a plain object (objects are not iterable by default)

  • Used the spread operator (...) on a non-iterable value

  • Tried to destructure a non-iterable as an array

Minimal example

✗ Broken code
const obj = { a: 1, b: 2 };
for (const item of obj) {
  console.log(item);
}
✓ Fixed code
const obj = { a: 1, b: 2 };
for (const [key, value] of Object.entries(obj)) {
  console.log(key, value);
}

Objects are not iterable with for...of. Use Object.entries() to get key-value pairs as an iterable.

How to diagnose

  • Check the type of the value you are iterating over

  • Use Object.keys(), Object.values(), or Object.entries() for objects

  • Verify the value has a Symbol.iterator method

How to fix

  • Use Object.keys(obj) or Object.entries(obj) to iterate over objects

  • Convert the value to an array first if it is array-like

  • Use for...in for object keys (with caution) or Object methods

How to prevent

  • Remember that for...of is for iterables (arrays, strings, Maps, Sets), not objects

  • Use Object methods for object iteration

Related Resources

Related Glossary

Related Lessons

← Back to language