JS: Test If Object is Iterator πŸš€

By Xah Lee. Date: .
xah_is_iterator(obj) return true if obj is an iterator

Note: this function is not 100% correct. It needs to test if the next function return value is an IteratorResult object.

Version 2023-01-16
const xah_is_iterator =
  ((x) => (Reflect.has(x, "next") && (typeof x["next"] === "function")));

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

// matchAll return iterator
  xah_is_iterator("number 344".matchAll("3")),

// array is not an iterator object
  xah_is_iterator([3, 4]) === false,

JavaScript, Iterable 🌟

BUY Ξ£JS JavaScript in Depth