I often see the habit:
var foo, bar;
for(var i = 0; i < 5; i++) {
foo = '' + foo + i;
}
It’s also rubbed off onto me, but I’ve just realized I have no idea why I do it.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There is no real problem with doing it, however javascript does not have block level scope, so if you declare foo inside the loop it’s still accessible throughout the whole function.
There is a small advantage when doing minification if you declare all your variables up front, consider:
In this case, when you minify there will be a lot more “var” statements appearing in your source. It’s not a huge deal, but they can certainly add up over time.