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 6659239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:00:36+00:00 2026-05-26T02:00:36+00:00

Some static languages like Java seem to have very special rules for variables defined

  • 0

Some static languages like Java seem to have very special rules for variables defined in the first argument of a for loop. They are accessible only by the given loop, which makes them behave pretty much like javascript functions’ local variables and arguments. I mean stuff like this:

class ForVariable {
  public static void main(String[] args) {
    for(int i = 0; i != 0; i++) {}
    System.out.println(i); // Throws an Exception
  }
}

Javascript doesn’t behave like that, which makes nesting loops quite a messy business. My question is: Is it valid to declare variables in the subsequent loops via the var keyword? In other words – which of the following examples is valid?

for(var i = 0, j; i < 5; i++) {
  for(j = 0; j < 10; j++) <do some stuff>;
}

OR

for(var i = 0; i < 5; i++) {
  for(var j = 0; j < 10; j++) <do some stuff>;
}

Clearly it is wrong to declare a variable several times, which would make the 2nd example a no-go, but given the fact that the 1st example is the way loops nesting is done in most languages I know, I’m rather hesitant to declare the winner.

  • 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-05-26T02:00:36+00:00Added an answer on May 26, 2026 at 2:00 am

    Its not wrong to declare a variable several times. For instance there is really no problem with:

    var i = 0;
    var i = 1;
    

    That’s valid JavaScript. Good tools like the Closure Compiler will generate a warning though because you typically don’t intend to do that.

    That being said, even the Closure Compiler won’t generate a warning for your example #2. It’s just common convention in JS even if you are technically re-declaring.

    Either of your two examples is fine but the second one is a little more sensible to parse. I wouldn’t worry about it either way.

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

Sidebar

Related Questions

Java has some very good open source static analysis tools such as FindBugs ,
Let's say I have some Java code: public class SomeClass { static { private
I'm wondering how static member variables are typically implemented in languages like C++ and
Some languages have the feature to return values using parameters also like C#. Let’s
Could people please rank some programming languages like C,C++,Java,perl etc on benchmark line of
I have some static images in a folder on my IIS 6-based website that
I have a web application (.war) that contains some static files (e.g. MS word
Suppose I have some per-class data: (AandB.h) class A { public: static Persister* getPersister();
I have some questions about the concept of static polymporhism I somethimes hear about;
Hey everyone! I am new to blackberry programming although I do have some java

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.