For statements in javascript for loop statements

  • 2020-03-30 02:35:07
  • OfStack

Loop statements are often used in program implementations, and for loops are found in most languages. There are several different USES of a for loop in javascript, and here's how I understand them.

Type 1 :(in general, perform related operations in a loop)
 
var objA=document.getElementsByTagName("a"); 
var i,max; 
for(i=0,max=objA.length;i<max;i++){ 
objA[i].onclick=function(){ 
alert(this.innerHTML); 
} 

} 

Loop, in turn, to register the click of the hyperlink label

Type 2 :(for objects, manipulate object content)
 
var person={name:'wmhello',age:'28'}; 
var tips=''; for(var obj in person){ 
tips+=obj+'-->'+person[obj]+'n' 

} 

alert(tips) 

Type 3 :(often used with arrays, performing specific operations on them)
 
var num=[1,3,5]; 
var total=0; 
num.forEach(function(e){ 
total+=e; 
}); 
alert(total); 

This forEach loop is available in firefox and chrome

Related articles: