Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
It's important to be able to look through an array to see what it holds. This video covers two ways of doing this: the for loop, and the forEach method. We'll compare and contrast the two techniques.
Node.js
Using Treehouse Workspaces for this course is strongly encouraged. However, if you'd like to work locally (on your computer), you'll need to install Node.js. Node allows you to run JavaScript from your command line. To install Node, you can go to this link for instructions:
Once you have Node installed, you'll need to use a console application on your computer to run the exercise file(s) described in the videos, and look at the output in your console. You'll also need a text editor like VS Code or Sublime Text to edit the file(s) before running them from the command line.
If all of that sounds like more than you want to do just yet, don't worry! Just click the "Launch Workspace" button below the video, and follow along.
Creating a Variable for an Array Element in a for Loop
Here's an example of creating a variable to hold each element in a for loop. As stated in the video, this is a bit more verbose than the forEach example we looked at. However, it can be helpful in making your intent easier to understand. Readable, intuitive code is a great thing!
const fruits = ['apple', 'pear', 'cherry'];
for (let i = 0; i < fruits.length; i += 1) {
const fruit = fruits[i]
console.log(fruit);
}
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
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