Home > Web Front-end > JS Tutorial > In-depth understanding of the loop statement for statement in javascript_javascript skills

In-depth understanding of the loop statement for statement in javascript_javascript skills

WBOY
Release: 2016-05-16 16:53:30
Original
1209 people have browsed it

Loop statements are often used in program implementation, among which the for loop is found in most languages. In JavaScript, there are several different uses of for loops. Here are my understandings of each.

First type: (usually, loop to perform related operations)

Copy code The code is as follows:

var objA=document.getElementsByTagName("a");
var i,max;
for(i=0,max=objA.length;iobjA[i].onclick=function(){
alert(this.innerHTML);
}

}

loop, registering hyperlink tags in turn Click operation

The second type: (for objects, operating object content)
Copy code The code is as follows :

var person={name:'wmhello',age:'28'};
var tips=''; for(var obj in person){
tips =obj '-->' person[obj] 'n'

}

alert(tips)

The third type: (commonly used for arrays, for arrays Perform specific operations)
Copy code The code is as follows:

var num=[1,3 ,5];
var total=0;
num.forEach(function(e){
total =e;
});
alert(total);

This forEach loop works in firefox and chrome
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template