What does this initialization of a variable stand for:
var variable = variable || {} ;
How and when should it be used?
What does this initialization of a variable stand for:
var variable = variable || {} ;
How and when should it be used?
That line of code does the following:
IF variable
is not defined (or has a falsey value) THEN set it to an empty object.
ELSE do nothing (technically speaking, variable
gets assigned to itself)
In other words variable
will be converted to an empty object if it is any of the following:
See toBoolean for the spec's definition of falsey values.
It is to test if variable
is initialized. If not, it initializes variable
as an empty object. If it does exist, it does nothing, (technically assigns variable
to itself).