So in that example I’ve changed
Am I creating a new var for each iteration?
Is there any difference in the following two ways of doing the same?
If so, why?
So, declaring a variable within a loop isn’t doing anything different than declaring it at the top of the function and then referencing it within the loop.
Note: the assignment to a variable is not hoisted, just the declaration of the variable. So, if you do this:
It works like this:
If you wanted to create a new scope inside your
Update in 2015. ES6 (or sometimes called ES2015) offers the
So, in ES6 if you did this: