isIterable

Checks if an object is iterable.

Checking an Array

const array = [1, 2, 3];
console.log(isIterable(array)); // Output: true

Checking a String

const string = 'Hello';
console.log(isIterable(string)); // Output: true

Checking an Object

const object = {"name":"John Doe","age":17};
console.log(isIterable(object)); // Output: false

Checking a Map

Maps are also iterables and allow for key-value pair iteration:

const map = new Map();
map.set('key1', 'value1');
console.log(isIterable(map)); // Output: true

Checking a Set

Sets, like Maps, are iterable collections but only store unique values:

const set = new Set([1, 2, 3]);
console.log(isIterable(set)); // Output: true

These examples illustrate how various data structures can be tested for iterability in JavaScript.

Last updated