-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloops.js
66 lines (54 loc) · 1.36 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
//for of loop mostly use for arrays and in for of no need of incrementation
const arr1=[1,2,3,4,5,6];
for (const num of arr1) {
// console.log(num);
}
const str="Samarpit";
for (const name of str) {
// console.log(str);
}
//maps are mot iteratable
const map=new Map()
map.set("USA","united state of amrica")
map.set("IN","india")
map.set("FR","france")
for (const key of map)
{
//console.log(key);//output as string
}
for (const [key,value] of map)
{
//console.log(key,":-",value);//not printing as string
}
for (const key of map)
{
// console.log(map);//print as many alement are there in map scope
}
//the forof loop is not supported for object
// so we use forof loop for object
const myobject=
{
MP:"Madhyapradesh",
MH:"maharastra",
}
for (const key in myobject) {
//console.log(`${key} :- ${myobject[key]}`);
}
const arr2=[1,2,3,4,5];
for (const value in arr2) {
//console.log(value,arr1[value]);
}
//foreach loop in it a callback function (without function name) is used with some parameters
const language=["Hindi","English","Panjabi","Tamil"]
language.forEach(function(value){
//console.log(value);
})
language.forEach((value)=>{
//console.log(value);
})
//calling function in for each loop
function printMe(items)
{
//console.log(items);
}
language.forEach(printMe)