Now, besides this for-let loop,
we also have a for-of loop.
And even though it sounds familiar,
it works totally differently.
Let's say we have an array of users,
and this is simply an array with a couple of names in there
like this.
And now we want to do something with every user
in a real website who might want to send an email to
the user, or just show that user on some dashboard screen.
Here I'll just lock these usernames.
Now, of course I can console log users.
And if I do that, it will work,
but it will simply lock the overall array.
That's not what I want here, instead
I just want to lock the individual names because again,
we might be doing more useful things here and send emails
to the individual users in stat.
And that's where a for-of loop can help us.
We again start with the 'for' keyword,
and then we again have brackets and curly braces.
But now between those parentheses here,
we don't write code as we did it up here,
but instead we create a helper
constant, user could be the name we want here.
The name is up to you.
It should describe your individual array elements though.
And since here I have users
an individual element could be named user.
And then we at the 'of' keyword there after
and then we point at the array for which we want to loop.
In my case to 'users' array.
And with that, we loop through all the elements in 'users'
and we get access to the individual elements
inside of that looping code under this helper constant.
And here it's a constant and not a variable because it's
basically a brand new variable that will be created for
every iteration whereas in the first kind of for loop,
it was the existing variable that was changed.
It's a slight difference.
That's why we can use the 'const' keyword here.
Of course you could also use 'let' alternatively,
but I'll use const since that is what
you'll see more often out there in the wild.
And then here I can console log user,
so not 'users', but 'user'
to log to individual elements for every iteration.
Now, with that, if I save that, you see here at the bottom,
Max, Anna, and Joel printed out as free individual elements
because this for-of loop now executed the code in here
three times, once for every element.
And it gave us access to every element.