1
- /*
2
- JavaScript Loops
3
1
4
2
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
3
6
4
There are four types of loops in JavaScript .
7
5
8
- for loop
9
- while loop
10
- do-while loop
11
- for-in loop
6
+ 1 ) for loop
7
+ 2 ) while loop
8
+ 3 ) do - while loop
9
+ 4 ) for - in loop
12
10
13
11
14
12
1 ) JavaScript For loop
@@ -19,35 +17,35 @@ There are four types of loops in JavaScript.
19
17
{
20
18
code to be executed
21
19
}
22
- */
20
+
23
21
24
22
for ( var i = 0 ; i <= 10 ; i ++ ) {
25
23
document . write ( i + "<br>" ) ;
26
24
}
27
25
28
- /* JavaScript while loop
26
+ 2 ) JavaScript while loop
29
27
30
28
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
29
32
30
while ( condition )
33
31
{
34
32
code to be executed
35
- } */
33
+ }
36
34
37
35
var i = 1 ;
38
36
while ( i <= 15 ) {
39
37
document . write ( i + "<br>" ) ;
40
38
i ++ ;
41
39
}
42
40
43
- /* JavaScript do while loop
41
+ 3 ) JavaScript do while loop
44
42
45
43
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
44
47
45
do {
48
46
code to be executed
49
47
}
50
- while (condition); */
48
+ while ( condition ) ;
51
49
52
50
var i = 50 ;
53
51
do {
@@ -56,12 +54,12 @@ There are four types of loops in JavaScript.
56
54
}
57
55
while ( i <= 52 )
58
56
59
- /* forEach loop
57
+ 4 ) forEach loop
60
58
61
59
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 */
60
+ for each loop by default not change your array but its change on copy of array so original array is same
63
61
64
- var sum = 0 ;
62
+ var sum = 0 ;
65
63
var arr = [ 10 , 18 , 12 , 20 ] ;
66
64
67
65
arr . forEach ( function myFunction ( element ) {
@@ -70,4 +68,3 @@ There are four types of loops in JavaScript.
70
68
} ) ;
71
69
72
70
73
-
0 commit comments