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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:15:46+00:00 2026-05-18T04:15:46+00:00

How should I create a changing variable as a global variable? so something like:

  • 0

How should I create a changing variable as a global variable?

so something like:

function globVar(variable){
   window.variable;
}

So in this way I could create global variables in an automatic mode too, and also I could create them for myself easier 🙂

EDIT

For example I could create a global variable just like this: globVar('myVariable'); then myVariable is added to the global variables.

  • 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-18T04:15:46+00:00Added an answer on May 18, 2026 at 4:15 am

    Sorry to say this, but the answers you have received are bad habits that you should stay away from. A better programming practice, and perhaps the proper programming practice, would be to pseudo-namespace your global variables so to not clutter the Global namespace/scope. The reasoning behind this is to make your code more manageable, and more importantly, make life easier for you if/when your application becomes large. A simple mechanism to define a namespace is to use the module-pattern made famous by Douglas Crockford.

    Here’s a simple example:

    var myNamespace = function(){
      var o = {};
      var globals = {};
    
      var setGlobVar = function(name, value) {
        globals[name] = value;
      };
    
      var getGlobVar = function(name) {
        if (globals.hasOwnProperty(name)) {
          return globals[name];
        } else {
          // return null by default if the property does not exist 
          return null;
        }
      };
    
      o.setGlobVar = setGlobVar;
      o.getGlobVar = getGlobVar;
      return o;
    }();
    

    To use this, you simply call it like methods of an object.

    myNamespace.setGlobVar("secret_msg", "Dumbledore dies, so does Hedwig");
    myNamespace.getGlobVar("secret_msg");
    

    You could also expose the globals variable instead of using the setGlobVar and getGlobVar methods to use it, if you wanted to simplify how you access the variable.


    The point is to stay away from defining variables in the global namespace (i.e., the window object) as much as possible, by creating a namespace of your own. This reduces the chance of name collisions, accidentally rewrites or overrides, and again, global namespace clutter.

    An even simpler approach to doing this is to simply define an object and augment its properties.

    var globals = {};
    globals.SECRET_MSG = "Snape is not a traitor"
    

    Though I would extend this approach by wrapping globals into a namespace that is specific to my application.

    var myNamespace = {};
    myNamespace.globals = {};
    myNamespace.globals.SECRET_MSG = "Snape is not a traitor"
    

    NOTE: This is actually the same as original module-pattern approach I suggested, just without the get and set accessor methods and coded differently.

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

Sidebar

Related Questions

I am trying to create a panel with changing pictures. This is my panel:
I'm learning MVC and am having trouble deciding when I should create a new
Should I create two CFile objects and copy one into the other character by
SHould I create a separate class for each object in an aggregate or should
Possible Duplicate: When to choose checked and unchecked exceptions When should I create a
For a company with many development projects, should you create multiple FitNesse wikis (one
Or should you always create some other lock object?
What's the best lifetime model for a DataContext ? Should I just create a
If an object has a property that is a collection, should the object create
You should be able to create a generic form: public partial class MyGenericForm<T> :

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.