JS: Test If Object is Iterable πŸš€

By Xah Lee. Date: .
/*
xah_is_iterable(obj) return true if obj is iterable.

Note: this function is not 100% correct. It needs to test if Symbol.iterator function return value is an iterator.

Version 2023-01-16
 */
const xah_is_iterable = ((x) => (
  (Reflect.has(x, Symbol.iterator)) &&
  (typeof (x[Symbol.iterator]) === "function")
));

// ssss---------------------------------------------------
// test

console.log(xah_is_iterable([3, 4, 5]));

console.log(xah_is_iterable({ kk: 3, [Symbol.iterator]: 4 }) === false);

JavaScript, Iterable 🌟

BUY Ξ£JS JavaScript in Depth