for, for ... in, for ... of the foreach loop and usage

1.for () loop

// for use between cycles of expression it is; and separating them, do not write,
for (initialization expression 1; Analyzing Expression 2; increment Expression 3) {
  // loop 4
}

2.for ... in indexes traversal

 

was obj1 = {
    name: 'John Doe',
    age : 17,
    sex: 'male'
}

for(var k in obj1){
    console.log(k);
    console.log(obj1[k]);
}

 

Note: Use the for ... in syntax, the same can iterate

Note: If a property or method name, is a variable, then use the object [variable name] Syntax

3.for ... of value to traverse

 

// iterate
var team = [ "master", "Big Brother", "two brothers", "sand Young", "Little White Dragon"];
for(var v of team){
      console.log(v);
}
// can traverse the string
var str = "zhangsan";
for (var v of str) {
      console.log(v);
}

 

Note: You can not traverse the object

4. The method of Array .forEach

 

array.forEach(v=>{  
    console.log(v);  
});
array.forEach(function(v){  
    console.log(v);  
});

If the programmers want to learn web front end, plus VX: TZED-21, free delivery web front-end video tutorial oh

Guess you like

Origin blog.csdn.net/weixin_44970764/article/details/90718159