You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The JavaScript loops are used to iterate the piece of code using for, while, do while or for-in loops. It makes the code compact. It is mostly used in array.
5
+
6
+
There are four types of loops in JavaScript.
7
+
8
+
for loop
9
+
while loop
10
+
do-while loop
11
+
for-in loop
12
+
13
+
14
+
1) JavaScript For loop
15
+
16
+
The JavaScript for loop iterates the elements for the fixed number of times. It should be used if number of iteration is known. The syntax of for loop is given below.
17
+
18
+
for (initialization; condition; increment)
19
+
{
20
+
code to be executed
21
+
}
22
+
*/
23
+
24
+
for(vari=0;i<=10;i++){
25
+
document.write(i+"<br>");
26
+
}
27
+
28
+
/* JavaScript while loop
29
+
30
+
The JavaScript while loop iterates the elements for the infinite number of times. It should be used if number of iteration is not known. The syntax of while loop is given below.
31
+
32
+
while (condition)
33
+
{
34
+
code to be executed
35
+
} */
36
+
37
+
vari=1;
38
+
while(i<=15){
39
+
document.write(i+"<br>");
40
+
i++;
41
+
}
42
+
43
+
/* JavaScript do while loop
44
+
45
+
The JavaScript do while loop iterates the elements for the infinite number of times like while loop. But, code is executed at least once whether condition is true or false. The syntax of do while loop is given below.
46
+
47
+
do{
48
+
code to be executed
49
+
}
50
+
while (condition); */
51
+
52
+
vari=50;
53
+
do{
54
+
document.write(i+"<br>");
55
+
i++;
56
+
}
57
+
while(i<=52)
58
+
59
+
/* forEach loop
60
+
61
+
its apply on array whenever you have array you can use foreach loop
62
+
for each loop by default not change your array but its change on copy of array so original array is same */
0 commit comments