Created
June 13, 2019 04:01
-
-
Save abirAbuAsim/43851888ca61b031ded313cf8c89c526 to your computer and use it in GitHub Desktop.
Weirdly enough, when you declare a var anywhere inside a method it gets the scope of the method's scope it resides in.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function simpleForLoopToShowVarBehavior() { | |
| for(var i = 0; i < 10; i++) { | |
| console.log(i); // You may think that i is scoped only inside this for loop | |
| } | |
| console.log(i); // Voila, for some magical reason you see '10' as output of this line | |
| // which means Javascript Var is not the variable declaration that you dreamt of | |
| // The i variable gets the value '10' from the last iteration of the for loop | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment