Checking whether something is iterable

The proper way to check for iterability is as follows:

function isIterable(obj) {
  // checks for null and undefined
  if (obj == null) {
    return false;
  return typeof obj[Symbol.iterator] === 'function';

Why this works (iterable protocol in depth):

Since we are talking about for..of, I assume, we are in ES6 mindset.

Also, don’t be surprised that this function returns true if obj is a string, as strings iterate over their characters.

Leave a Comment