I have a question about JavaScript's namespacing. What are the advantages of namespace?When shall I use namespace? Is it bad habit to use global variables?
-
Isolation, when you want to isolate something and usually yes :) – Ja͢ck Apr 22 '13 at 17:05
-
duplicate of http://stackoverflow.com/questions/4246284/why-are-globals-bad – Aaron Kurtzhals Apr 22 '13 at 17:08
4 Answers
It is a bad practice to store all of your variables in the global namespace, mainly because you open yourself up for name clashes. For example, if you define a function named Foo
and you import a JavaScript file that also defines Foo
, they'll clash and one will end up being hidden when called.
The advantages of namespacing is similar to other languages:
- you can logically group your code
- you can organize your functions into smaller, reusable blocks
- you can avoid name clashes
- you avoid cluttering the global namespace (which leads to the anti of the above items)
I believe that anytime you start writing JavaScript, you should avoid adding your functions to the global namespace and keep your code in your own namespace:
var MyLibrary = MyLibrary || {};
MyLibrary.x = function() { ... };
It's a good habit to get into.

- 28,421
- 8
- 67
- 102
-
1Although we all know the correct answer to this, I think your answer is the clearest and most concise. +1 – BBagi Apr 22 '13 at 17:08
var company = company || {} ;
company.doSomething = function() {
};
company.test = {};
company.test.submodule = {};
company.test.submodule.doSomething = function() {};
company.api = {};
company.api.submodule = {};
You should generally avoid defining variables globally. For clear maintainable code, use objects as namespaces and avoid polluting the global namespace.
This also greatly enhances default JS functionality. If you are really keen on it, add something like require.js to your projects and you will get a very nice Java-like functionality.
So with require.js, your company.api and company.test can be placed in different files and require each other, just like you would do an import company.test with a Java package.
Namespacing is a very efficient practice. You get:
- Readability(the most important thing in the world!!).
- Maintainability.
- Faster debugging.
- Faster code upgrades/code delivery.
- Lazy-loading and minifying is easier, loads of tools can be used for this.
- Efficient division of logic and functionality.
- Prevents IE window vs global object bugs.
- Makes working in a team possible and pleasant.
- Speeds up development A LOT. Most IDEs will index and autofill function name etc as you type based on your namespaces.
- You can effectively use jsDoc tags to document your JavaScript code.
- You can avoid collisions with other libraries.

- 28,161
- 11
- 65
- 105
In general the benefits of namespacing are:
- Limiting pollution of the global scope and preventing naming collisions
- Providing context for your function names (we'd expect different arguments and results for WindowUtils.getHeight and MenuUtils.getHeight for instance).
You can use namespaces whenever you're building a project of any significant size or complexity.
Globals are generally a problem because its easier for naming collisions and unintended side effects between different files.

- 25,260
- 12
- 52
- 71
It is always bad to use global variables if you can avoid it as it clutters a namespace that you have no control over. Where this can be bad is if a new plugin is introduced, or a second programmer works on your code, and global variable names are shared, you're going to get very undesirable and hard-to-trace errors.
By using your own namespacing, you can isolate your functions and variables into a unique, safe little cocoon. :D

- 2,035
- 1
- 19
- 23