Your question assumes that you do have control over whether your variable declaration is the "initial" declaration. If you do have that control, then there is never a reason not to use var
except when you are working inside of a function and specifically want to access a global variable and not create a local variable. In that case you must not use var
or else the variable will become a local variable and you can never access the global variable with the same name from within that function.
When you are using variables in the global scope, you are technically making an error if the same variable name is declared twice using var. There is no way to control this from happening sometimes, which is why you will run into situations where you "can't" use var
.
Update: the type of error I am referring to is something like this: error: Identifier 'x' has already been declared
This doesn't happen anymore (thankfully). It happened way too often in the late 1990s when JavaScript was new. Perhaps it was an error of the browser implementations. At at rate, don't worry about redeclaring variables with var
anymore. (Redeclaring variables with let
is still not permitted. For the let
keyword, Redeclaring the same variable within the same function or block scope raises a SyntaxError.
)
Let me explain the history of the practice behind my word "can't":
Starting with the old-fashioned "Hit-o-Meter" scripts, JavaScript programmers started noticing that their scripts generated error messages (for anyone looking at the console) when used in the "wild" when many different script elements in the page might use the same global variables. The solution to avoid the errors from showing up was to simply not declare the variable. (A warning would show up when the variable was implicitly declared by the first assignment to it, but all the other errors that happened by multiple variable declarations in many places on the page would be gone. So it was a question of a lesser evil versus a greater evil.)
If you have full control over all the scripts in the page, then you will have full control as your phrase says, "initially declaring a JavaScript variable." For example, you can use var
when declaring a global variable in the top script, but not use var
when using the same variable in another script element later in your page.
If you have no way of controlling whether a variable with the same name might have been declared before, and if you don't want to commit the possible evil of using a global variable which has never been initialized even by a prior script element, then you have the option that I use of using properties of "self" or other convenient window or document properties. For instance, I will write self.variable = 1
, which will be free of any problems associated with redefining a variable. There are no errors or warnings generated regardless of whether this variable is being used for the first time or being re-used by my script after already being used by another script on the page.
(Note: currently self
and window
are considered to be equivalent, but there was a time in the days of framesets where it was considered better style to use self
to store any global variables/properties--and it also saved a tiny bit of code.)