At w3schools there is written:
If you declare a variable, without using "var", the variable always becomes GLOBAL.
Is it useful to declare global variable inside the function? I can imagine to declare some global variables in some event handler, but what is it good for? Better usage of RAM?
No, there's no RAM benefit or anything like that.
What w3schools is talking about is something I call The Horror of Implicit Globals. Consider this function:
Seems simple enough, but it returns
NaN
, not11
, because of the typo on thevaraible2 = 6;
line. And it creates a global variable with the typo'd name:This is because the function assigns to
varaible2
(note the typo), butvaraible2
isn't declared anywhere. Through the mechanics of the scope chain in JavaScript, this ends up being an implicit assignment to a (new) property on the global object (which you can access aswindow
on browsers).That's just a "feature" of loose-mode JavaScript, assigning to a completely undeclared identifier isn't an error; instead, it creates a propertly on the global object, and properties on the global object are global variables. (Up through ES5, all globals were properties of the global object. As of ES2015, though, a new kind of global was added that isn't a property of the global object. Global-scope
let
,const
, andclass
create the new kind of global.)My example is a typo, but of course, you could do it on purpose if you wanted. It's a clearly-defined part of the language, after all. So:
...anywhere that
myNewGlobal
isn't declared will create the new global.But I would strongly recommend never doing it in purpose: It makes the code hard to read and maintain, and that code will be incompatible with JavaScript modules when they become more common and widespread. If you really need to create a global variable from within a function at runtime (already a red flag, but there are valid reasons for it), do it explicitly by assigning to a property on
window
(or whatever refers to the global object in your environment; it'swindow
on browsers):In fact, I'd suggest using ES5's strict mode. Strict mode makes assigning to an undeclared identifier an error rather than silently creating a global. If we'd been using strict mode, the problem with
foo
above would have been much easier to diagnose:Somewhat tangential, but in general I'd recommend avoiding globals wherever possible. The global namespace is already very, very cluttered on browsers. The browser creates a global for every element in the DOM with an
id
, for most elements with aname
, and has several predefined globals of its own (liketitle
) which can easily conflict with your code.Instead, just define yourself a nice scoping function and put your symbols in it:
And if you do that, you might want to enable strict mode:
...which, as mentioned, has the advantage of turning assignments to undeclared identifiers into errors (along with various other helpful things).
Note that in a JvaScript module (added in ES2015, but only now beginning to find their way into the wild), strict mode is enabled by default. (This is also the case with
class
definitions, also new in ES2015.)Sometimes its useful to create new globally accessible properties inside functions which can later be easily accessed by referencing the window object (all globally declared properties are attached to the window object).
However as it usually is with declaring anything to be globally accessible it can lead to problems later because those properties can be easily overwritten etc. Its far better to simply pass values to functions as arguments and retrieve their results.
The main problem is someone else may already be using a global with the same name.
Then when you change the value of the global you'll overwrite their value.
Later on when the global is next used it will have mysteriously changed.
I would say that it might hurt your security and even stability of your code.
As was mentioned above, you might make a mistake by simply misspelling your variables and a solution is the keyword
"use strict";
With this keyword declared it will throw you an error:
Uncaught ReferenceError: foo is not defined
.It also refers to a secured code:
1. When writing a secured code, we do not want our variables to be accessed anywhere besides where they were actually declared. Do not declare global variables without the need.
2. Always read warnings carefully and resolve them. Use
"use strict";
, JSlint and other tools to see and resolve warning to make your code better.Side Effects When Forgetting var
There’s one slight difference between implied globals and explicitly defined ones. The difference is in the ability to undefine these variables using the delete operator:
• Globals created with var (those created in the program outside of any function) cannot be deleted.
• Implied globals created without var (regardless if created inside functions) can be deleted.
This shows that implied globals are technically not real variables, but they are properties of the global object. Properties can be deleted with the delete operator whereas variables cannot:
In ES5 strict mode, assignments to undeclared variables (such as the two antipatterns in the preceding snippet) will throw an error.
JavaScript Patterns, by Stoyan Stefanov (O’Reilly). Copyright 2010 Yahoo!, Inc., 9780596806750.
The only use of global variables is if you need to access them globally. In that case you should declare them using the
var
keyword outside the functions, to make it clear that you really want to create global variables, and didn't just forget thevar
when trying to declare a local variable.Generally you should try to scope your code so that you need as little as possible in the global scope. The more global variables you use in your script, the less is the chance that you can use it along side another script.
Normally variables in a function should be local, so that they go away when you exit the function.