Lingua-e
← Errors
JavaScript

Spread Non-Iterable: What It Means and How to Fix It

Error reference

Spread Non-Iterable

Language

JavaScript

Severity

Medium

When it happens

This TypeError is thrown when the spread operator (...) is used on a value that does not implement the iterable protocol.

Potential fixes

Guard the value before spreading with a fallback: [...(value ?? [])]. Verify that API responses that should be arrays are actually arrays. Use Array.isArray() to validate before spreading. Remember: object spread ({...obj}) and array spread ([...arr]) have different requirements.

Deep dive

This TypeError is thrown when the spread operator (...) is used on a value that does not implement the iterable protocol. Arrays, strings, Sets, and Maps are iterable; plain objects, null, undefined, and numbers are not.

Why it happens

  • Spreading null or undefined into an array literal.
  • Spreading a plain object ({}) in an array context (only works in object spread).
  • Spreading a value from an API that returned null instead of an array.
  • Attempting to spread a number or boolean value.

Potential fixes

Guard the value before spreading with a fallback: [...(value ?? [])]. Verify that API responses that should be arrays are actually arrays. Use Array.isArray() to validate before spreading. Remember: object spread ({...obj}) and array spread ([...arr]) have different requirements.

Examples

JavaScript

Code that triggers the error

const a = [1, 2];
const b = null;
const merged = [...a, ...b];

Error output

TypeError: b is not iterable

Fixed code

const a = [1, 2];
const b = null;
const merged = [...a, ...(b ?? [])];
console.log(merged); // [1, 2]

Practice in English

How would you explain a Spread Non-Iterable to a fellow dev? Choose the right phrase:

"I hit an error...

Ready to practice your English at work?

Lingua-e has interactive exercises built around real developer conversations: standups, code reviews, retrospectives, and more. Practice until it comes naturally.

Try Lingua-e for free