Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8841863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:49:26+00:00 2026-06-14T10:49:26+00:00

I often see the habit: var foo, bar; for(var i = 0; i <

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:49:27+00:00Added an answer on June 14, 2026 at 10:49 am

    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:

    // Up front
    var a, b, c, aVal, bVal, cVal;
    
    for (a = 0; a < 5; ++a) {
        aVal = a;
    }
    
    for (b = 0; b < 5; ++b) {
        bVal = b;
    }
    
    for (c = 0; c < 5; ++c) {
        cVal = c;
    }
    
    // Inline
    for (var a = 0; a < 5; ++a) {
        var aVal = a;
    }
    
    for (var b = 0; b < 5; ++b) {
        var bVal = b;
    }
    
    for (var c = 0; c < 5; ++c) {
        var cVal = c;
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I often see code a function defined without visibility keywords. e.g: class Foo() {
I often see a pattern used in circumstances where we have look-up code that
I often see it mentioned that Thread.Sleep(); should not be used, but I can't
I often see in third party JavaScript code that after: var el = document.getElementById(elementId);
I often see this in code: var me = this; . Why is that?
I often see people claiming their backend is implemented in Django, but isn't Django
I often see the following code: (function () { // init part })(); but
I often see code like: Iterator i = list.iterator(); while(i.hasNext()) { ... } but
I often see the code like this: public abstract class AbstractDataReader { public void
I often see two conflicting strategies for method interfaces, loosely summarized as follows: //

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.