Now we also have a for in loop.
And the difference is that for in
is meant to be used on objects.
Let's say we have a loggedInUser object here,
and I'm just coming up with some example data here.
We're still of course
just exploring some dummy use cases here.
And that loggedInUser has a name,
which is Max,
age which is 32.
And an isAdmin property,
which is true.
And here we see a boolean value
created and assigned just like this,
because that is also a common use case for booleans
that we store them just like this
in variables or properties that act as flex.
So that tells us whether a user is an admin or not,
for example.
And now that I got these free properties,
we could output these key value pairs here
with help of a for in loop.
If we had some code that needs to access
all these properties
and transform the data into some other format,
or do anything else with them.
For this again,
we started with the for keyword,
and then again we try to help with constant,
but this constant will then actually hold
the different property names,
or keys as we can also call them.
And hence you very often named this key,
But the name is up to you.
We could also name it propertyName.
The important thing to note,
it's just that it will be the name,
not the value and not both together.
Just the name.
Then we use the in keyword here,
and then we point at the object
through which we want to loop.
So in this case for propertyName in loggedInUser,
Now in here we can now console log propertyName
to see what's stored in there,
and if we save that code,
you will see name, age and isAdmin
being printed out here at the bottom.
Because these are the three property names
we have in here.
Now, if you want to get access to the values as well,
that's possible with help all of a special kind of syntax
which we haven't seen yet.
We can console log the loggedInUser,
and now dynamically access the different properties
stored in the user.
Up to this point,
when we wanted to access a property,
we always did that with the dot notation.
To access loggedInUser.name,
for example.
But that won't work here
since the property I want to access
is actually different in every execution.
And to access such property names dynamically,
we can use a syntax
which we already know from erase.
We can't add square brackets here after loggedInUser,
even though that's not an array but an object,
but there you can use square brackets as well.
And between those square brackets,
you can put the property name as a string.
So for example,
name, I could access this like that as well.
And that would be equivalent to loggedInUser.name.
But since we have this syntax,
we can now also not hard-code the propertyName
as a string here,
but instead refer to propertyName.
our helper constant here.
And property name indeed holds the
different property names as strings.
So therefore this line of code will dynamically access the
values for those different propertyNames,
which are different for every execution.
That's how we can access propertyNames dynamically.
This does not work with the dot notation
because of you had loggedInUser.propertyName,
it would look for a property named propertyName,
not having the name stored as a value
inside of propertyName,
so that won't work.
But with the square bracket notation,
we can dynamically access the values
by that dynamic propertyName.
And hence, if we save that,
we get name, Max,
age, 32
and is Admin, true.
So that's then working as it should.