-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVar Vs Let.js
28 lines (21 loc) · 1.34 KB
/
Var Vs Let.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
for(var i =0;i<5;i++)
{
setTimeout(()=>{console.log(i)},1)
}
// In this code snippet, we have a for loop that iterates five times. The loop variable i is declared
// using the var keyword, which means it has function scope rather than block scope. The loop iterates from i = 0 to i < 5
// When the setTimeout functions execute after the loop has completed,
// the value of i is 5 at that time (since the loop has already finished, and i has reached the value 5).
// So, all five functions will log the value 5 to the console.
for(let i =0;i<5;i++)
{
setTimeout(()=>{console.log(i)},1000)
}
// In this case, the loop variable i is declared using the let keyword, which means it has block scope.
// Each iteration of the loop will create a new variable i, and this variable will only be accessible inside the block of the loop.
// With let, each iteration creates a new variable i with block scope.
// So, when each setTimeout function executes after the delay,
// they reference their respective i variable from their respective loop iteration.
// In summary, the main difference between the two versions is the scoping behavior of the loop variable i.
// Using var results in function scope, causing the value of i to be shared across all iterations,
// while using let results in block scope, providing each iteration with its own separate variable.