Skip to content

Instantly share code, notes, and snippets.

@abirAbuAsim
Created June 13, 2019 04:01
Show Gist options
  • Select an option

  • Save abirAbuAsim/43851888ca61b031ded313cf8c89c526 to your computer and use it in GitHub Desktop.

Select an option

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.
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