From the course: Computer Science Principles Lab: JavaScript

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Use loops with arrays

Use loops with arrays - JavaScript Tutorial

From the course: Computer Science Principles Lab: JavaScript

Start my 1-month free trial

Use loops with arrays

- [Instructor] When you have values in an array, it is common to want to perform actions on each one of the items. You can use a for loop to iterate through all the items in the array and access each element individually. There are two ways to do this. The first is with a standard for loop. When you create a for loop, you need to define the iterator variable, and then use that value to test if the loop should continue or not. Each array has a property called length that you can use in your for loop conditional test to see if the iterator variable exceeds the length of the array. If I have an array like this one, there are four elements, but remember, the last element has an index number of three, so it is one less than the length of the array. We can use this to build our for loop. We start the for loop like any other, for. We create the iterator variable. for var i equals zero. But in our test, we want to verify that the iterator is less than the value of the array length property…

Contents