Why does JavaScript loop only use the last value?
The seemingly quirky behavior of loops in JavaScript using the last value when var is employed is a result of function-scoped variables.
In the JavaScript world, it's not uncommon to encounter unexpected behaviors, especially for those coming from other programming languages. One such puzzling behavior involves loops seemingly using only the last value of an iteration.
Let's delve into this intriguing phenomenon, elucidate the reasons behind it, and look at the pros and cons of this behavior with example code.