Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fce6e40

Browse files
loops
1 parent 132eb85 commit fce6e40

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

‎loops.js‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
JavaScript Loops
3+
4+
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 (var i = 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+
var i = 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+
var i = 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 */
63+
64+
var sum = 0;
65+
var arr = [10, 18, 12, 20];
66+
67+
arr.forEach(function myFunction(element) {
68+
sum = sum + element;
69+
console.log(sum);
70+
});
71+
72+
73+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /