hello and welcome to texure tutorials uh
this is a miniseries on iterators and
generators um in this series the first
part would be understanding the
iterables and iterators so here it is
and the next tutorial I'll go through
generators and its use now this is a
little bit of complex subject so in
order to explain in a simple language
it's a little but I'll try my best and
as I said always try to understand the
fundamentals uh before moving on to
complex stuff arrays in JavaScript are
considered iterable because you have
elements and you have index and you can
iterate through each element using for
Loop uh or like for each uh however if
you have an object such as such as this
where you have key value pairs uh this
is not considered as a trable now how do
you know what is a trable and what is
not so recently uh the latest vers of
JavaScript introduced uh um something
called symbol. iterator it's a property
in iterable objects and you can find out
using this so if I take an array like
this and I can look it inside uh what
property it has in its prototype
using console
dot
DOD this should give me the detail on
this this object
and if I run this I should get uh array
and I should get something called Proto
within that I have all the Prototype
methods for it and one of the method is
actually called symbol. iterator uh this
has recently added to all the iterable
objects if I do the same thing for this
particular object and if I run
this I would get uh this object here and
if I look at this
prototype I would now not get
anything uh I would not get that method
symbol. Trader so that is the difference
between um iterable objects and non-
iterable objects this is a non iterable
um javascri recently introduced a few
new data structures uh JavaScript has
now sets weak sets map and weak Maps uh
these are all iterables um I'm not going
to go go through all of them but I'm
just going to pick one like sets sets
are um array like objects uh with with
only accepts unique
values so for example I can create a new
set uh so I can say let
my set equal to I have to use a new
keyword and set with S capital and
within that I can Define sort of an
array and so it's 1 2 2 2 3
okay now what other because of a set it
only accepts uh unique keys so it will
throw out anything that is console
di my
set I should see the same behavior so if
I run
this I should get this set um and you
can see the values it has this uh
entries field where you have uh unique
values one two and three so and if you
look at his prototype I should see the
same thing symbol or
iterator um another thing about this
symbol or iterator it allows you to use
a new for Loop called for off uh how
does this work so I can say I can Loop
through the elements of this set using
four off so I can say four and the way I
would say is let I create a variable
here um value off and and off of this
set so my
set and I can say
console.log
V now this should print one 2 and three
because it's a set it doesn't print all
the values because it doesn't have it
cannot do duplicates so if I run this I
would get one two and
three I cannot use the same thing on
this object um however it does have a
method called for in so I can say
four let Val
off um an object and I should say
actually key because it prints all the
keys um if I say
console log key
it should print all the
keys uh actually I should say as I said
it won't work because it gives you error
because it as you can see it says unod
type error symbol. trator is not a
function uh so it won't work on this uh
I can do
in if I clear this and run this again I
would get all the keys name age friends
and get name um however there is a
weakness to this it it would print out
all the the keys including anything that
is defined by user in its prototype from
the iterable now let's move to iterator
uh so we looked at this uh a new
property that was introduced called
symbol. iterator uh that is only
available in iterable objects uh like
string sets um you know uh so you can
use this particular property is nothing
but a function um it's a function that
is available to all the itable objects
uh you can run this function to get a
iterator out so the way you can do it is
um if I Define an array uh my array
equal to I can Define 1 2 3 4
5 and I can say
um iterator
equal to uh
my
array
and symbol
dot
iterator and I can ex execute this
function uh so this is this this
function is available already in the Pro
prototype of this array and I can run
this function um and I would get an
iterator so this iterator is very
interesting I can actually um run a next
method on this iterator and it would
give me each value um one by one so the
way it would work is I can say
iterator and do next I can actually
console lock this
um if I run
this it gives me two things it give me
the it will it gives me the value the
first value of this array which is one
and it would give me another property
called done uh using this property I can
determine if I'm at the end of this
array or not right now I'm at the
beginning because it has five elements
total right it won't give me done equal
to false until I get to the last element
so if I print this five
times I will get uh five different
values I would get one the first 2 3
four five and every time done is still
false I have reached last element which
is five and done is still false um now
if I cut it again now the value will be
undefined and done will be true so at
this point I will stop um now this
iterator is really useful uh when you
use it uh in combination with generators
so that would be the next uh tutorial uh
you can follow the link after this
tutorial to go into this next tutorial
and that that's when we learn about
generators and real use of this iterator
and if you learn something from this
tutorial uh please provide a
constructive comment uh like the video
and subscribe the channel for future
cool videos