JavaScript中的循环

Loops? Well like the name suggests, these are the block which repeats itself when some certain conditions are met. So, we will discuss here some day to day most useful loops.

循环? 顾名思义, 这些是满足某些特定条件时会重复出现的块 。 因此,我们将在这里每天讨论最有用的循环

For循环 (The For Loop)

For loop have the following format for(initialization, condition, updation) now, what it means? We see an example here:

for循环现在具有以下格式(初始化,条件,更新) ,这意味着什么? 我们在这里看到一个例子:

for(let i = 0; i<10; i++){
	console.log(i);
}

Now we observed that the let i = 0 is a part of initialization that means i will be declared for this for block. i < 10 is the condition here so whenever i will be lesser than 10 the for block will run, and last i++ simply a shorthand for i = i + 1 that is variable i will increase by one every time it completes the block.

现在我们观察到let i = 0是初始化的一部分,这意味着将为此for块声明i 。 i <10是这里的条件,因此每当我小于10时,都会运行for块,最后一个i ++只是i = i + 1的简写,它是可变的,每次完成该块, i都会加一。

While循环 (The While Loop)

Just like for loop, while loop is also a most useful loop. The format for while loop is while(condition).

就像for循环一样, while循环也是最有用的循环。 while循环的格式为while (condition) 。

Let’s see an example,

我们来看一个例子

let i = 10;
while(i>0){
	console.log(i);
	i--; //line 1
}

Here, we already have a variable before while loop, and it only needs one condition, in this case, i must be greater than 10. Now in line 1 we used i-- which is a shorthand for i = i – 1 and like you can notice we make an update by ourselves in itself the while loop and it’ll run till the condition is true.

在这里,我们在while循环之前已经有一个变量,并且只需要一个条件,在这种情况下, 我必须大于10。现在在第1行中,我们使用了i--这是i = i – 1的简写,例如您会注意到,我们自己在while循环中进行了一次更新,它将一直运行直到条件为真。

Now we begin one of the most useful methods for the arrays, that is, 'forEach()'

现在我们开始对数组最有用的方法之一,即“ forEach()”

This forEach method is like any other loop whether a for loop or a while loop.

此forEach方法类似于任何其他循环(无论是for循环还是while循环)。

week.forEach(function(days,index){
	console.log('day ${index+1} is',days);
})

forEach performs the specified action for each element in an array. The forEach method accepts 2, arguments first a callback function and another thisArg.

forEach为数组中的每个元素执行指定的操作。 forEach方法接受2个参数,首先是一个回调函数,然后是另一个thisArg 。

Callback function: Well we can call a function after declaring it as an argument of forEach(). But a callback function is what we declare at the time of calling. When we use a callback function in forEach we can use at most 3 arguments, first for values/elements of the array and second for index and last one as a string.

回调函数好吧,我们可以在将函数声明为forEach()的参数之后调用该函数。 但是回调函数是我们在调用时声明的。 当我们在forEach中使用回调函数时,我们最多可以使用3个参数,第一个用于数组的值/元素 ,第二个用于索引 ,最后一个作为字符串 。

thisArg: An object to which this keyword can refer in the callback function. If thisArg is omitted, undefined is used as this value.

thisArg:此关键字可以在回调函数中引用的对象。 如果省略thisArg , 则将 undefined用作此值。

forEach() method will call the callback function for each element of the array, the days (in above example) will hold one element at a time and index will hold the index for the corresponding element.

的forEach()方法将调用回调函数的阵列的每个元件,所述天(在上面的例子)将在时间保持一个元件和索引将保持索引为对应的元件。

Thus, from the above code, we have the following output:

因此,从以上代码中,我们得到以下输出:

for each output

To understand more what are the callback functions, read: Understanding callbacks in JavaScript

要了解更多什么是回调函数 ,请阅读: 了解JavaScript中的回调

翻译自: https://www.includehelp.com/code-snippets/loops-in-javascript.aspx

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值