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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:01:23+00:00 2026-06-04T17:01:23+00:00

Consider the following code: for (var i=0; i<100; i++) { // your code here

  • 0

Consider the following code:

for (var i=0; i<100; i++) {
    // your code here
}
// some other code here
for (var i=0; i<500; i++) {
    // custom code here
}

Any decent lint tool (jslint, jshint or built in IDE) will tell warning – duplicate declaration of variable i. This can be solved by using variable with another name (k, j) or moving declaration to the top:

var i; // iterator
for (i=0; i<100; i++) {}
for (i=0; i<500; i++) {}

I am not fond of both variants – I don’t make declarations at the top usually (and even if I did I wouldn’t want see there helper variables – i, j, k) and really nothing bad is going on in those examples to change variables’ names for.

Though I do want a huge warning in case I write something like this:

for (var i=0; i<100; i++) {
    for (var i=0; i<500; i++) {} // now that's bad
}

What’s your approach to such cases?

  • 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-04T17:01:24+00:00Added an answer on June 4, 2026 at 5:01 pm

    JavaScript has many constructions which look like well-known constructions in other computer languages. It’s dangerous for JavaScript to interpret the construction in another way as most other computer languages.

    If somebody who doesn’t know JavaScript good enough (the common case by the way) sees the construction like

    for (var i=0; i<100; i++) {
        // your code here
    }
    

    or sees declaration of the variable in the block

    {
        var i;
        //some code
        {
            var j;
            // some code
        }
    }
    

    then most readers will think that block level variables will be defined. JavaScript don’t have block level variables. All variables will be interpreted as function level defined.

    So I never define variables inside of the code if the code is not just some test code which will be written for 5 min only. The main reason is that I don’t want to write code and use language constructions which could be misunderstood.

    By the way JSLint finds defining of variables inside of block such bad style that it stop processing of the code analysis. There are no JSLint options which could change this behavior. I find the behavior of JSLint not good, but I agree that declaration of variables inside of for loop is bad because it will be read by most persons as code with local loop variables, which is incorrect.

    If you use

    for (var i=0; i<100; i++) {
        // your code here
    }
    // some other code here
    for (var i=0; i<500; i++) {
        // custom code here
    }
    

    then JavaScript moves all declarations of variables at the beginning of the function for you. So the code will be as

    var i = undefined, i = undefined; // duplicate declaration which will be reduced
                                      // to one var i = undefined;
    
    for (i=0; i<100; i++) {
        // your code here
    }
    // some other code here
    for (i=0; i<500; i++) {
        // custom code here
    }
    

    So please think about other readers of the code. Don’t use any constructions which could be interpreted in the wrong way.

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

Sidebar

Related Questions

Consider the following script for outputting some XML code: var xmlAsString = '<?xml version=1.0?><person><name
Consider the following code var myData = {name: 'John'}; function fixName(data) { var newData
Consider the following code: var Widget = new Class({ Implements: [Options], options: { name
Consider the following code: var sentences = [ 'Lorem ipsum dolor sit amet, consectetur
Consider the following code: class Foo(var name: String = bar) Now i try to
Consider the following code: [Authenticate(Order = 1)] public ActionResult SomeActionThatRequiresAuthentication() { var model =
I am trying to work with arrays in javascript. Consider the following code: var
Consider the following Javascript code: var a = []; var f = function() {
Consider the following code: var Products_First = (from Entities.Product p in myContext.Product select p);
Consider the following code. var items = from i in context.Items select i; var

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.