xxxxxxxxxx
// Sample Data:
const days = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
// Samle Data which has undefined items:
const days = ["Monday","Tuesday",,"Thursday",,"Saturday","Sunday"]
// This Post might be a good read, to see a few caveats and advantages:
// https://stackoverflow.com/questions/500504/why-is-using-for-in-for-array-iteration-a-bad-idea
//Classic For Loop using 3 expressions iterates all array indexes:
for (let i=0; i<days.length; i++){
console.log("Day: "+days[i])
}
//for..in iterates defined array indexes:
for (let day in days){
console.log("Day: ", days[day]);
}
//for..of iterates all array items:
for (let day of days){
console.log("Day: ", day);
}
//using array methods might be more suitable depending on the scenario:
days.forEach(function(day){
console.log("Day: ", day);
});
//available methods:
days.forEach()
days.map()
days.filter()
days.reduce()
days.every()
days.some() //this one is neat imo
xxxxxxxxxx
var nums = [1, 2, 3, 5];
for (var i = 0; i,nums.length; i++){
console.log(nums[i]);
}
xxxxxxxxxx
for (let index = 0; index < array.length; index++) {
const element = array[index];
}
xxxxxxxxxx
var arr = [1, 2, 3, 4];
for (var i = 0; i < arr.length; i++) {
console.log(arr[i]);
};
xxxxxxxxxx
var listItem = [
{name: 'myName1', gender: 'male'},
{name: 'myName2', gender: 'female'},
{name: 'myName3', gender: 'male'},
{name: 'myName4', gender: 'female'},
]
for (const iterator of listItem) {
console.log(iterator.name+ ' and '+ iterator.gender);
}
xxxxxxxxxx
var sum = 0;
for (var i = 0; i < a.length; i++) {
sum + = a[i];
} // parsing an array
xxxxxxxxxx
var arr = [1, 2, 3, 4];
arr.forEach(function(element) {
console.log(element);
});
xxxxxxxxxx
let i;
for (i = 0; i < 6; ++i) {
console.log("Hello"); // print Hello 6 times
}