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

  • Home
  • SEARCH
  • 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 6977269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:36:16+00:00 2026-05-27T17:36:16+00:00

Possible Duplicate: Javascript dynamic variable name A very basic question. I want to create

  • 0

Possible Duplicate:
Javascript dynamic variable name

A very basic question. I want to create a new javascript global variable each time a function is called. The variable should contain the id of the element so that I can easily access it later.

id = 2347

//this function would be called multiple times, hopefully generating a new global each time
function (id)
{
var + id = something
// I want a variable that would be named var2347 that equals something, but the above line doesn't get it.
}

In a later function, I want to access the variable like so:

function two (id)
{
alert(var + id);
}

I’m sure I’m going to have a “doh!” moment when someone is kind enough to answer this.

  • 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-27T17:36:17+00:00Added an answer on May 27, 2026 at 5:36 pm

    How about…

    var store = (function() {
        var map = {};
    
        return {
            set: function ( name, value ) {
                map[ name ] = value;
            },
            get: function ( name ) {
                return map[ name ];
            }
        };
    })();
    

    Usage:

    store.set( 123, 'some value' );
    

    and then…

    store.get( 123 ) // 'some value'
    store.get( 456 ) // undefined
    

    Live demo: http://jsfiddle.net/jZfft/

    Programmers are highly advised to not declare global variables, since the browsers already ship with several hundreds of names in the global namespace. Using the global namespace for your own variables can lead to name-collisions which can then break the program or some of the browser’s functionality. Creating new namespaces is free, so don’t be shy to do it…

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

Sidebar

Related Questions

Possible Duplicate: Javascript Regexp dynamic generation? I am trying to create a new RegExp
Possible Duplicate: Javascript create variable from its name The code below checks to see
Possible Duplicate: Javascript Objects: Dynamic variable names? I have a json string passed in
Possible Duplicate: Use javascript variable in object name I am using CKeditor as a
Possible Duplicate: JavaScript: Scroll Position of div with “overflow: auto” Hi, i want to
Possible Duplicate: JavaScript equivalent to printf/string.format How can I create a Zerofilled value using
Possible Duplicate: Javascript scoping variables theory Hi all, I want to ask something stranger.
Possible Duplicate: Javascript Array.sort implementation? I want to know how .sort() works in JavaScript.
Possible Duplicate: Dynamically loading an external CSS file I'm trying to create a dynamic
Possible Duplicate: javascript domready? I want to check whether $(function(){ }); is ready. Return

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.