-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFor_vs_for-of_vs_for-in.js
70 lines (63 loc) · 960 Bytes
/
For_vs_for-of_vs_for-in.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
// for vs for of vs for in
var arr = [10,20,30,40,50];
undefined
// simple for loop
for(var i = 0 ; i<arr.length; i++){
console.log(arr[i]);
}
VM305:3 10
VM305:3 20
VM305:3 30
VM305:3 40
VM305:3 50
undefined
// for of (Iterator)
for(var a of arr){
console.log(a);
}
VM445:2 10
VM445:2 20
VM445:2 30
VM445:2 40
VM445:2 50
undefined
// for in (Object Traverse)
// best for key value access
typeof arr;
'object'
for(var key in arr){
console.log(key);
}
VM620:2 0
VM620:2 1
VM620:2 2
VM620:2 3
VM620:2 4
undefined
for(var key in arr){
console.log(key, arr[key]);
}
VM657:2 0 10
VM657:2 1 20
VM657:2 2 30
VM657:2 3 40
VM657:2 4 50
undefined
var emp = {id:1001, name:'Amit', address:'Delhi'};
undefined
typeof emp;
'object'
for(var key in emp){
console.log(key);
}
VM906:2 id
VM906:2 name
VM906:2 address
undefined
for(var key in emp){
console.log(key, emp[key]);
}
VM940:2 id 1001
VM940:2 name Amit
VM940:2 address Delhi
undefined