JavaScript: For , For/in , For/of

 For:

define:

The for statement can customize how many times you want to execute code

Grammar:

  for (condition 1; condition 2; condition 3)
  {
        code
  }
 

Condition explain:

condition describe
1 prestart before beginning.
2 loop condition.
3 Execute after the loop has been executed.

 

 

 

 

 

 

Example:

for (var i=0; i<5; i++)
{
      x=x + "The number is " + i + "<br>";
}

The code 's output:

    The number is 0
    The number is 1
    The number is 2
    The number is 3
    The number is 4

 


 

For/in:

define:

The for/in statement is used to loop through the object properties。

Grammar:

  for (var in object) {
     code
  }

parameter values:

parameterdescribe
var

Must.variable can be an array element or an attribute of an object.

(指定的变量可以是数组元素,也可以是对象的属性。)

object

Must.Object that specifies the iteration.

(指定迭代的的对象。)

 

 

 

 

 

 

 

 

 

Example:

1 var string = {lesson:"web", verb:"is", describe:"fun"}; 
2 
3 var text = "";
4 var x;
5 for (x in string) {
6 
7     text += string[x]+" ";
8 }

The code 's output:

    web is fun 

 


 

For/of:

define:

The for/of statements creates a loop iterating over iterable objects.

Grammar:

  for (variable of iterable) {
      statement
  }

parameter values:

parameterdescribe
variable

On each iteration a value of a different property is assigned to variable.

(在每次迭代中,将不同属性的值分配给变量。)

object

Object whose iterable properties are iterated.

(被迭代枚举其属性的对象。)

 

 

 

 

 

 

 

Example:

1 let iterable = [10, 20, 30];
2 
3 for (let value of iterable) {
4     value += 1;
5     console.log(value);
6 }

The code 's output:

    11 21 31

 


 

 Related Pages:

JavaScript for    :          http://www.runoob.com/js/js-loop-for.html

JavaScript for/in :         http://www.runoob.com/jsref/jsref-forin.html 

MDN - for...of :              https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/for...of

Javascript statement :   http://www.runoob.com/jsref/jsref-statements.html

posted @ 2018-11-11 20:08  陈府  阅读(142)  评论(0编辑  收藏  举报