-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoops.html
64 lines (55 loc) · 1.88 KB
/
Loops.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Loops</title>
</head>
<body>
<div class="container">
This is about loops
</div>
<script>
console.log("We'll learn to make to loops here");
let i = 0;
for (i = 0; i < 3; i++) { //Here i++ increases i by 1 units.
console.log(i);
}
let friends = ['Aman', 'Vaibhav', 'Maynak', 'Varun', 'Sanjukta'];
for (let index = 0; index < friends.length; index++) {
console.log('Hello friend ' + friends[index]);
}
//Doing the same thing we have done above but by creating a new function
friends.forEach(function f(element) { //Defining function forEach
console.log('Hello my friend ' + element + ' to modern JavaScript')
});
//Using for-of loop to do the same
for (element of friends) {
console.log('Hello friend' + element + ' again to modern JavaScript');
}
let employee = {
name: 'Aman',
degree: 'B.Tech',
age: 18,
class: 'Freshmen'
}
// This loop is used to iterate over objects in JavaScript (for-in loop)
for (key in employee) {
console.log(`The ${key} of employee is ${employee[key]}`);
}
// While loop in JavaScript
// let i =0;
while (i < 7) {
console.log(`${i} is less than 7`);
i++;
}
// do while loop in js
let j = 34;
do {
console.log(`${j} is less than 4 and we are inside do while loop`);
j++;
} while (j < 4);
</script>
</body>
</html>