Possible Duplicate:
What does “options = options || {}” mean in Javascript?
What does the following line of Javascript code do?
var somevar = window.somevar || {};
Possible Duplicate:
What does “options = options || {}” mean in Javascript?
What does the following line of Javascript code do?
var somevar = window.somevar || {};
It sets somevar
to window.somevar
if window.somevar
exists and is not boolean false
, otherwise it sets it to an empty object {}
It's a common idiom for handling variables which may not have been set.
It's an either/or assignment. If window.somevar
is false
or undefined
, then somevar
is set equal to {}
. Otherwise it's set equal to window.somevar
.
This will evaluate the expression window.somevar
as a boolean expression. If it evaluates to true
then it will return the value of window.somevar
. If it evaluates to false
then it will return the empty object {}