1.

Implement your version of Array.prototype methods forEach, map and filter in vanilla JavaScript ?

Answer»

To understand iterators, we will first LOOK into recently introduced [Symbol.iterator] property in data types. 

This indicates whether a data structure is iterable or not. This includes array, strings, Map, Sets and NodeList. Objects doesn’t have a [Symbol.iterator] property.

If we check the __proto__ of an array, we can find the [Symbol.iterator] property.

But it is not the CASE with Objects as it is not iterable.

Now, iterator are created using the “Symbol.iterator” and can be used to iterate over all data STRUCTURES which have [Symbol.iterator] property.

Let consider the below example. Here we create an iterator by the syntax at line 3. Now we can iterate over the array using “iterator.next()”. Each run gives and OBJECT which have the “value” and “done”, which specifies whether there is an ELEMENT. Notice, after the fifth run we get the “value” as undefined and “done” as true.



Discussion

No Comment Found