difference let and var 1

difference let and var

In simple words 'var' is function scoped and 'let' is block scoped

Here is what the above code is Doing:
1. We have a function named ‘foo’ which has a variable named ‘a’ declared using ‘var’ keyword.
2. We have a ‘for’ loop which has a variable named ‘a’ declared using ‘let’ keyword.
3. We are trying to print the value of ‘a’ declared using ‘var’ keyword inside the ‘for’ loop.
4. We are trying to print the value of ‘a’ declared using ‘let’ keyword outside the ‘for’ loop.

Now, let’s see what happens when we run the above code:
1. The value of ‘a’ declared using ‘var’ keyword inside the ‘for’ loop is printed as ‘undefined’.
2. The value of ‘a’ declared using ‘let’ keyword outside the ‘for’ loop is printed as ’10’.

Why is this happening?
1. The ‘var’ keyword is function scoped.
2. The ‘let’ keyword is block scoped.
3. The ‘for’ loop is a block.
4. The ‘var’ keyword is hoisted to the top of the function.
5. The ‘let’ keyword is not hoisted to the top of the function.
6. The ‘var’ keyword is hoisted to the top of the function and is initialized with ‘undefined’.
7. The ‘let’ keyword is not hoisted to the top of the function and is not initialized.
8. The ‘var’ keyword is hoisted to the top of the function and is initialized with ‘undefined’.
9. The ‘let’ keyword is not hoisted to the top of the function and is not initialized.
10. The ‘var’ keyword is hoisted to the top of the function and is initialized with ‘undefined’.
11. The ‘let’ keyword is not hoisted to the top of the function and is not initialized.
12. The ‘var’ keyword is hoisted to the top of the function and is initialized with ‘undefined’.
13. The ‘let’ keyword is not hoisted to the top of the function and is not initialized.
14. The ‘var’ keyword is hoisted to the top of the function and is initialized with ‘undefined’.
15. The ‘let’ keyword is not hoisted to the top of the function and is not initialized.
16. The ‘var’ keyword is hoisted to the top of the function and is initialized with ‘undefined’.
17. The ‘let’ keyword is not hoisted to the top of the function and is not initialized.
18. The ‘var’ keyword is hoisted to the top of the function and is initialized with ‘undefined’.
19. The ‘let’ keyword is not hoisted to the top of the function and is not initialized.
20. The ‘var’ keyword is hoisted to the top of the function and is initialized with ‘undefined’.
21. The ‘let’ keyword is not hoisted to the top of the function and is not initialized.
22. The ‘var’ keyword is hoisted to the top of the function and is initialized with ‘undefined’.
23. The ‘let’ keyword is not hoisted to the top of the function and is not initialized.
24. The ‘var’ keyword is hoisted to the top of the function and is initialized with ‘undefined’.
25. The ‘let’ keyword is not hoisted to the top of the function and is not initialized.
26. The ‘var’ keyword is hoisted to the top of the function and is initialized with ‘undefined’.
27. The ‘let’ keyword is not hoisted to the top of the function and is not initialized.
28. The ‘var’ keyword is hoisted to the top of the

Similar Posts