-
Notifications
You must be signed in to change notification settings - Fork 1
/
outPuttracing.js
86 lines (69 loc) · 2.17 KB
/
outPuttracing.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const a = [1,2,3,4,5,6,7,8,9,10]
for(var i=0; i<10; i++){
setTimeout(function(){
console.log(a[i]);
},1000)
}
//OUTPUT : undefined 10 times
// This is because the setTimeout function is an asynchronous function
// that will not block the execution of the rest of the code. Therefore,
// when the console.log statement inside the function is executed after the delay
// of 1 second, the value of i will have already reached 10, which is outside the
// index range of the array a. As a result, a[i] will evaluate to undefined for all iterations.
const b = [1,2,3,4,5,6,7,8,9,10]
for(let i=0; i<10; i++){
setTimeout(function(){
console.log(b[i]);
},1000)
}
//OUTPUT : 1,2,3,4,5,6,7,8,9,10
for(var i=0; i<10; i++){
(function(i){
setTimeout(function(){
console.log(i);
},1000 * i)
})(i)
}
for(var i=0; i<5; i++){
(function(j){
setTimeout(function(){
console.log(j);
},1000 * j)
})(i)
}
//Output : 0,1,2,3,4,5,6,7,8,9 same output with var also
function func(){
var x = "variable"
function abc(){
console.log(x)
var x
}
return abc();
}
func()
//Output : undefined
// This is because of a concept called variable hoisting in JavaScript.
// When a variable is declared with the var keyword, the declaration is "hoisted"
// to the top of the current function or global scope. This means that the variable
// declaration is moved to the beginning of the scope, before any other code is executed.
// However, the value of the variable is not hoisted, only the declaration.
// In the code above, the variable x is declared inside the abc function,
// but the declaration is hoisted to the top of the abc function. Therefore,
// when the console.log(x) statement is executed, x exists but has not been assigned
// a value yet, so it has the value undefined
function func(){
var x = "variable"
function abc(){
var x;
console.log(x);
x = "new value";
}
return abc();
}
(function(){
console.log(1)
setTimeout(function(){console.log(2)}, 10000)
setTimeout(function(){console.log(3)},0)
console.log(4)
})()
//1,4,3,2