javascript code to loop through array code example
Example 1: javascript loop through array
var data = [1, 2, 3, 4, 5, 6];
for(let i=0; i<=data.length; i++) {
console.log(data[i])
}
for(let i of data) {
console.log(i)
}
for(let i in data) {
console.log(i)
console.log(data[i])
}
data.forEach((i) => {
console.log(i)
})
data.map((i) => {
console.log(i)
})
Example 2: javascript code to loop through array
var colors = ["red","blue","green"];
for (var i = 0; i < colors.length; i++) {
console.log(colors[i]);
}
Example 3: loop an array in javascript
let array = ["loop", "this", "array"];
for (let i = 0; i < array.length; i++) {
console.log(array[i]);
}
Example 4: iterate through array javascript
var myStringArray = ["Hello","World"];
var arrayLength = myStringArray.length;
for (var i = 0; i < arrayLength; i++) {
console.log(myStringArray[i]);
}
Example 5: javascript loop through array
let my_array = [1, 2, 3, 4, 5];
for(let i = 0; i < my_array.length; i++) {
console.log(my_array[i])
}
for(let i of my_array) {
console.log(i)
}
Example 6: 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 }` );