Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
The simplest (and arguably the fastest) way to loop over an array is
to use the while loop.
Below, we loop from the beginning of the index to the end.
<!DOCTYPE html><html lang="en"><body><script> var myArray = ['blue', 'green', 'orange', 'red']; var myArrayLength = myArray.length; /* cache array length, to avoid unnecessary lookup */ var counter = 0; // setup counter while (counter < myArrayLength) { // run if counter is less than array length console.log(myArray[counter]); // logs 'blue', 'green', 'orange', 'red' counter++; // add 1 to the counter } </script></body></html>
And now we loop from the end of the index to the beginning.