-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloops.js
70 lines (47 loc) · 2.16 KB
/
loops.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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.
There are four types of loops in JavaScript.
1)for loop
2)while loop
3)do-while loop
4)for-in loop
1) JavaScript For loop
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.
for (initialization; condition; increment)
{
code to be executed
}
for (var i = 0; i <= 10; i++) {
document.write(i + "<br>");
}
2) JavaScript while loop
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.
while (condition)
{
code to be executed
}
var i = 1;
while (i <= 15) {
document.write(i + "<br>");
i++;
}
3) JavaScript do while loop
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.
do{
code to be executed
}
while (condition);
var i = 50;
do {
document.write(i + "<br>");
i++;
}
while (i <= 52)
4) forEach loop
its apply on array whenever you have array you can use foreach loop
for each loop by default not change your array but its change on copy of array so original array is same
var sum = 0;
var arr = [10, 18, 12, 20];
arr.forEach(function myFunction(element) {
sum = sum + element;
console.log(sum);
});