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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:44:31+00:00 2026-06-11T19:44:31+00:00

I hear limit your global variables i javascript. If I have to declare variables

  • 0

I hear limit your global variables i javascript.

If I have to declare variables in global scope then I will do something like this:

var global1 = 0, global2 = 1, global3 = 2, global4 = 3;

Here I have four global variables. Now I thought of limiting the numbers of global variables and did some thing like this

var globals = {
    global1 : 0,
    global2 : 1,
    global3 : 2,
    global4 : 3
}

I wanted to know whether the second method is better or just the same, since by exposing the variable globals I am exposing the other others also.

  • 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-11T19:44:32+00:00Added an answer on June 11, 2026 at 7:44 pm

    Your approach is basically called namespacing and is done by most big libraries like, e.g., jQuery uses jQuery and/or $ to hold all of its variables and functions.

    The main reason for reducing the amount of global vars is to limit possible collisions, if different scripts use the same variable names.

    So, e.g., script A defines a global variable cache for its internal purposes. When now a second, unrelated script B needs another global variable of that name, you will run into trouble as probably script B will just overwrite the variable of script A.

    By putting your variables (and functions as well) into a global namespace of your own with a name that probably is not used by any other script, you reduce the probability of the aforementioned to happen.

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

Sidebar

Related Questions

This is something I am not sure about in core data. Suppose I have
I hear this term used quite frequently, but have yet to see it specified
Hear is my code. I would like to get picture with api php facebook.
I hear D is link-compatible with C. I'd like to use D to create
You hear terms like classpath, build path, etc. all the time, referencing a list
I often hear things like Can we load our employee info using LDAP? Yet,
I hear all this stuff about crazy ways to build iPhone apps using Ruby
I hear a lot of Emacs users have their init file (.emacs or init.el)
I hear i should convert the dict to a propertylist and then send it
I hear this phrase often and do not fully understand it's meaning. What does

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.