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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:44:35+00:00 2026-05-22T12:44:35+00:00

Perhaps I’m not aware of how for loop index variables get scoped, but I

  • 0

Perhaps I’m not aware of how for loop index variables get scoped, but I was very surprised when one of my loops didn’t complete, seemingly because a function called from within a loop contained an i for its for loop index as well.

Here’s a little script I put together to demonstrate this behavior:

var loopOne = function(test) {
    for(i = 0; i < test.length; i++)
        console.log(getMask(test));
};

var getMask = function(pass) {      
    var s = "";
    for (i = 0; i < pass.length; i++) {
        s = s + "*";
    }
    return s;      
};

loopOne('hello');

If I run this in Chrome and look at the console log, I should see ***** five times. However, I only see it once. Upon further inspection, if I type i in the Chrome javascript console, it will output 6 ( = 'hello'.length + 1). This makes me think that i has become a part of the global scope and is not limited to the scope of the for loop for which it was needed.

Is this correct? If so, what’s a better practice for defining the index variable of a for loop in javascript?

  • 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-22T12:44:36+00:00Added an answer on May 22, 2026 at 12:44 pm

    In Javascript, variables are scoped with the var keyword. When declaring variables with var, the variable is scoped to the current function. When assigning to a variable without using the var keyword, it is assumed you’re talking about an already defined variable in the same or a higher scope. If none is found, the variable is created in the highest scope.

    Bottom line: declare all your variables using var.

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

Sidebar

Related Questions

Perhaps very simple, but not for me ;) How do i change the Picture
Perhaps I'm not using the right terminology, but I'm struggling to try to find
Perhaps a dump question (But I'm not a super experienced user of .NET), but
Perhaps I am missing something but how does one go about retrieving the xml
Perhaps I am not getting SQL Parameters correctly, but this doesn't work for me.
Perhaps not the best title, but here is the issue. We are creating a
Perhaps it is very basic, but I have some issue with the radios.length property.
Perhaps not directly programming related, but definitely product / commercially related. And I can't
Perhaps this is an effect of installing the new 4.5 beta, but my simulator
Perhaps I'm a bit confused about when to use the waitForPageToLoad command, but what

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.