Index
Array Iteration Examples


We'll play with the fundamental and fun concept of iterating over the elements in an array.

Indexed iteration is similar to many other programming languages.

let coll, sum;

coll = [10, 20, 30, 40];
sum = 0;
for (let i=0; i<coll.length; i++) {
	sum += coll[i];
}
prn("sum: " + sum);
Here is an enhanced (simpler) iteration approach when the index is not needed

let coll, sum;

coll = [10, 20, 30, 40];
sum = 0;
for (let eachElement of coll) {
	sum += eachElement;
}
prn("sum: " + sum);
Functional programming provides us with this option.

let coll, sum;

coll = [10, 20, 30, 40];
sum = 0;
coll.forEach(eachElement => sum += eachElement);
prn("sum: " + sum);

More Example Sources


Here are a couple (among many more) online related example sources: