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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:25:47+00:00 2026-05-27T12:25:47+00:00

Is there a shortcut for writing the following 100 assignments? variable_1 = 1; variable_2

  • 0

Is there a shortcut for writing the following 100 assignments?

variable_1 = 1;
variable_2 = 2;
variable_3 = 3;

...

variable_100 = 100;

I have tried

for(var i = 1; i <= 100; i++) {
    variable_ + i = i;
}

but I get the error message “Invalid left-hand side in assignment”. Any ideas?

  • 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-27T12:25:47+00:00Added an answer on May 27, 2026 at 12:25 pm

    Here are a few methods:

    Method 1: use eval

    Here is the most direct method:

    for(var i = 1; i <= 100; i++) {
      eval("var variable_" + i + " = " + i);
    }
    variable_1; // => 1
    

    Disclaimer for the above method: I don’t think this problem is a good candidate for using eval. If you do use eval, you should never allow user input to go into what you are evaling, or you could open your site to security risks. That mistake is the main reason people say eval is evil.

    Method 2: use dynamically generated object properties

    This is a much, much better way:

    // If you want these variables to be global, then use `window` (if you're 
    // in a browser) instead of your own object.
    var obj = {};
    for(var i = 1; i <= 100; i++) {
      obj["variable_" + i] = i;
    }
    obj.variable_1; // => 1
    

    About the note in the comment about using window to create global variables: I would recommend against this, as it is a quick way to pollute your global scope and step on variables unwittingly.

    Method 3: use an array

    David suggested using an array. This is another great idea, and, depending on what you are trying to do, may be preferred:

    var arr = [];
    for(var i = 1; i <= 100; i++) {
      arr.push(i);
    }
    arr[0]; // => 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a shortcut for writing the following mySQL query? I am looking for
I was wondering whether there was a way to do the following, without writing
Is there a shortcut key to bring up the call hierarchy of a method
Is there a shortcut to add an event method for a control? If I
C#: Is there a shortcut to detect if mouse is still hovering on top
If I ran a search pattern and nothing was found, is there a shortcut
Is there a keyboard shortcut in Access 2003 that will run a query while
Is there a single shortcut to start inserting in the new line at end
Is there a Vim shortcut for jumping to the argument list of the current
Is there a keyboard shortcut for the Connect to Server modal window in Microsoft

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.