Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Like forEach
, for...of
creates loop that repeats an action a given number of times.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Another way you might write a loop is
with the for of loop, like for each,
0:00
for of creates a loop that repeats
an action a given number of times.
0:04
You create a for
of loop with the keyword, for
0:09
followed by a parentheses and
curly braces.
0:14
Inside the parentheses, you provide for
0:18
of a variable that represents the current
item being processed in the loop.
0:21
This is very similar to the movie
parameter we just used inside for each.
0:25
So I'll again call the variable
movie with var movie, and
0:30
you could also the let and const keywords
I mentioned earlier here in place of var.
0:35
Now I'll follow the variable
with the keyword of and
0:40
the name of the array we're looping over,
in this case, movies.
0:44
Inside the curly braces
you provide a statement or
0:49
action to be carried out for each
element that gets passed into the loop.
0:52
I'll simply log each movie
title to the console
0:57
with console.log and pass it in movie.
1:00
So as you can see, this loop
statement is pretty simple to read.
1:05
It almost reads in plain English, for
1:09
every movie of the movies array,
log it to the console.
1:13
I'll give this a save,
refresh the browser, and
1:19
now over in the console we see a list of
all the titles stored in the movies array.
1:23
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up