1

I am learning about iteration in es6 and i understand what it does. However, i constantly come across the words "it returns an iterable". If an object looks like this { } and an array like this [ ] etc can someone please show me what a iterable looks like. Thanks

Yossi Sternlicht
  • 740
  • 1
  • 6
  • 17
  • 1
    Don't hesitate to read the docs as they are pretty clear: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols – kemicofa ghost Jan 24 '19 at 10:44
  • The question doesn't really have much sense, `iterable` is more like a property than a raw type, you cannot represent it by a definition syntax. By the way, avoid thinking in terms of "looks like" when talking about "syntax to define a type of object". – Kaddath Jan 24 '19 at 10:47

5 Answers5

3

Baiscally an iterable is a value, even a primitive value where Symbol.iterator is implemented.

For example an array has this by default, like

var array = ['a', 'b', 'c'];

console.log(...array);

But if you add the iterator, for example, to the prototype of Number, you could even spread numbers.

Number.prototype[Symbol.iterator] = function* () {
    for (var i = 0; i < this; i++) {
        yield i;
    }
};

console.log(...10);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Iterable means an object (an array is also an object) that has the function Symbol.iterator. To simply test whether an object is iterable or not, you can try to use the spread operator on it

example iterable example To understand how iterable works https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/iterator

gafi
  • 12,113
  • 2
  • 30
  • 32
0

An iterable by itself is not a data structure (like an array or an object). Instead it is a property function of an object which defines how the object is iterable.

See Iteration Protocols for details:

The iterable protocol allows JavaScript objects to define or customize their iteration behavior, such as what values are looped over in a for..of construct. Some built-in types are built-in iterables with a default iteration behavior, such as Array or Map, while other types (such as Object) are not.

str
  • 42,689
  • 17
  • 109
  • 127
0

When they are saying "it will return iterable" that means you can use it in the for...of loop to iterate that Object. As we know that Plain JS Object is not iterable. Below are the iterables values: 1.Arrays 2.Strings 3.Maps 4.Sets An iterable is a data structure that wants to make its elements accessible to the public. It does so by implementing a method whose key is Symbol.iterator. That method is a factory for iterators.

-1

Iterable is interface, not data structure. You can read more here

Adam W.
  • 1
  • 1
  • Perhaps giving an explanation that is back up with a documentation link would have been better answer. – Alex Leo Jan 24 '19 at 10:55
  • You are right, but I presumed that he has read documentation. I found explanation in this blog good for layman so I thought it might be useful. – Adam W. Jan 24 '19 at 11:08