loop to go through array javascript code example
Example 1: loop through an array javascript
let array = ['Item 1', 'Item 2', 'Item 3'];
for (let index = 0; index < array.length; index++) {
console.log(array[index]);
}
for (let index in array) {
console.log(array[index]);
}
for (let value of array) {
console.log(value);
}
array.forEach((value, index) => {
console.log(index);
console.log(value);
});
Example 2: javascript loop through array
array iteration styles illustrate programming paradigms
let newArray = []; const theArray = [2, 4, 6, 8, 10]
for (var i = 0; i < theArray.length; i++) {
console.log(theArray[i]);
newArray[i] = theArray[i] * theArray[i]
}
function f1(theValue, theIndex, originalArray) { numVal = numVal * theValue }
theArray.forEach(f1); console.log( ` creates ${ newArray }` );
run.addEventListener("click", function () {cpeople.forEach((element) => console.log(element.firstname));});
function f1(v,i,a) { v=v*v }
const constArray = theArray.map(fNumbers);
console.log( ` immutable ${ newArray }` );