O'Reilly logo

JavaScript Recipes: A Problem-Solution Approach by Keith Cirkel, Russ Ferguson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

© Russ Ferguson and Keith Cirkel 2017
Russ Ferguson and Keith CirkelJavaScript Recipes10.1007/978-1-4302-6107-0_8

8. Working with Arrays in Loops

Russ Ferguson and Keith Cirkel2
(1)
Ocean, New Jersey, USA
(2)
London, UK
 

How Do You Use a for Loop on an Array ?

Problem

You want to iterate through all the values of an array-like object where order is important.

Solution

A for loop lets you iterate through the values of an array in numeric order.

The Code

var alphaArray = ['a', 'b', 'c', 'e', 'd', 'e', 'f', 'g'];
var arrayLength = alphaArray.length;
for (let i = 0; i < arrayLength; i++){
     console.log(alphaArray[i]); //returns a,b,c,d,e,f,g
}
Listing 8-1.
Iterating Through an Array-Like Object with a For Loop

How It Works

For loops are often used when iterating ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required