javascript loop through array
const array = [1, 2, 3, 4];
for(num of array) {
console.log(num);
}
javascript loop through array
const array = [1, 2, 3, 4];
for(num of array) {
console.log(num);
}
javascript loop through array
array iteration styles illustrate programming paradigms
let newArray = []; const theArray = [2, 4, 6, 8, 10]
// 1) imperative, explicit
for (var i = 0; i < theArray.length; i++) { // var is mutable
console.log(theArray[i]); // side effect from writing to console
newArray[i] = theArray[i] * theArray[i] // let is mutable
}
// 2) forEach function of the Array class TIP: console.log(Array) //Does not change the array, Returns undefined
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));});
// 3) declarative, implicit
function f1(v,i,a) { v=v*v }
const constArray = theArray.map(fNumbers);
console.log( ` immutable ${ newArray }` );
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us