docs/reference/predicate/isIterable.md
Checks if a value is iterable.
const result = isIterable(value);
isIterable(value)Use isIterable to check whether a value implements the iterable protocol — that is, whether it has a Symbol.iterator method. Arrays, strings, Set, Map, typed arrays, and generators are iterable; plain objects, null, and undefined are not.
import { isIterable } from 'es-toolkit/predicate';
// Iterable values
console.log(isIterable([1, 2, 3])); // true
console.log(isIterable('abc')); // true
console.log(isIterable(new Set([1, 2, 3]))); // true
console.log(isIterable(new Map())); // true
// Non-iterable values
console.log(isIterable({ a: 1 })); // false
console.log(isIterable(123)); // false
console.log(isIterable(null)); // false
It can also be used as a type guard in TypeScript.
function collect(value: unknown): unknown[] {
// Inside this branch, `value` is narrowed to `Iterable<unknown>`
if (isIterable(value)) {
return [...value];
}
return [];
}
value (unknown): The value to check.(value is Iterable<unknown>): Returns true if the value is iterable, false otherwise.