I'm working with JavaScript code and I find some lines that do this operation:
var a = a || [];
What I have seen in all those lines that I have identified is that the pattern is the same: declare a variable and assign to it the value of the newly created operation or variable against an empty array
What is the purpose of doing this?
The point of doing that is to keep the value of the variable if it had already been defined before.
In this particular case (
var a = a || [];
) what is being done is that if the variablea
had already been defined previously in the code, it is left as it was (a
). But if it hasn't been defined yet, it is initialized to an empty array ([]
).Note that javascript, when you duplicate a variable declaration, does not issue any warning. It simply reduces both declarations to a single declaration at the beginning of the function.
is reduced as
var a = undefined;
at the beginning of the function.Now, in a function where
a
it is declared as an argument, the same thing happens, you would be duplicating the declaration, and javascript will reduce the duplicate to a single declaration.to the answer
This is a technique used in ECMAScript 5 to declare the default value of arguments.
Example: (ECMAScript 5)
Personally I think that it is not very recommended to use
var
when you already defined the variable as an argument. It is better to leave it without the wordvar
.This is the least error-prone way. A programmer not expert in javascript could believe, by common sense, that the second declaration of
a
, hides the first, which would be a mistake on his part.Consider this example:
As I already said, the declaration moves to the beginning and is
var a
not declaring a new variable that is only valid inside the if. It is the same variablea
for the whole function. Unlike other languages.Finally, in ECMAScript 2015 the technique is no longer necessary because you can declare the default value in the function declaration like this:
All the answers so far are correct, but none explain why so I'm going to add it.
The operator
||
, as in many other languages, is the logical disjunction operator , that is, an OR operation . Now, in JavaScript, this operator doesn't returntrue
orfalse
as you might expect, but instead returns the first value that evaluates to true in the string . If such a value does not exist, that is, all arguments are false , the last value in the string will be returned.So,
var a = a || []
it works as follows:a
:a
contains a true value , its value is returned.a
contains a false value (for exampleundefined
, if the argument was not passed), evaluation continues.[]
:Thus, at the highest level, we have: 1
a
defined, it returns its value.a
it is not defined, it returns[]
.As stated previously, this is generally used to declare default values for function arguments. Special care must be taken with this pattern if the function accepts any of the following values as valid:
null
false
0
''
(anstring
empty)All of these values evaluate to false, so we might get false positives. Of course, if these values are the default values, nothing will change: it will
a || 0
return0
whether it is defined or nota
.0
If any of those values are valid, use the following pattern instead:
Of course, use the desired default value instead of
[]
.It becomes something like:
If the variable exists
a
and is defined, it leaves the variablea
as it was, and if it is not defined, it assigns a reference to a newarray
void.It is used to define a default value in case it
a
does not existIt is also the way to define default values in function parameters.