© Russ Ferguson and Keith Cirkel 2017

Russ Ferguson and Keith Cirkel, JavaScript Recipes, 10.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

Listing 8-1. Iterating Through an Array-Like Object with a For Loop
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}

How It Works

For loops are often used when iterating ...

Get JavaScript Recipes: A Problem-Solution Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.