//pass an array of numbers into a function and log each number to the consolefunctionyourFunctionsName(arrayToLoop){//Initialize 'i' as your counter set to 0//Keep looping while your counter 'i' is less than your arrays length //After each loop the counter 'i' is to increased by 1for(let i =0; i <arrayToLoop.length; i++){//during each loop we will console.log the current array's element//we use 'i' to designate the current element's index in the arrayconsole.log(arrayToLoop[i])}}//Function call below to pass in example array of numbersyourFunctionsName([1,2,3,4,5,6])
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);}
/*
for (statement1; statement2; statement3) {
INSERT CODE
statement1 is executed before for loop starts
statement2 is the condition
statement3 is executed after every loop
}
*/for(i =0; i <5; i++){console.log(i);}
// 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...
let items =['beef','cabbage','javascript'];// Defines a list of itemsfor(let item of items){// Defines for loop with a variable of 'item'console.log(item);// Prints the item that is found}
const user ={name:'Shirshak',age:25,subscibers:200,money:'lolno'}for(let x in user){console.log(user[x])//name,age,subscriber and money(only get key not value)}
Howfor-loops work
Afor loop has 3 parts
I will go the through the first section, than the third and than the second.The first part is where we will start,inthiscase,I want to start at 0.for(let index =0;)
than we say every time the loop repeats how much does it add?InthiscaseIm using numbers and adding 1 each time.soIsay:for(let index =0; index = index +1)And the final part when do we want the loop to stop?inthiscaseI want it to stop at 10 so I will make my for-loop like this:for(let index =0; index <10; index = index +1)NowI add the body to my for-loop
for(let index =0; index <10; index = index +1){}And now inside the body I run the command:console.log(index);this will run the for-loop
for(let index =0; index <10; index = index +1){console.log(index);}//-> 0 1 2 3 4 5 6 7 8 9It will run to 9 not 10 because it did run 10 times, but
the index started at 0 not 1